blob: f2dfc0ebdcd81d5a4b13842ef883a1455d7838d9 [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"
16#include "qemu/error-report.h"
Zhang Chen59509ec2016-09-27 10:22:27 +080017#include "trace.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080018#include "qapi/error.h"
19#include "net/net.h"
Zhang Chenf4b61832016-09-27 10:22:31 +080020#include "net/eth.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080021#include "qom/object_interfaces.h"
22#include "qemu/iov.h"
23#include "qom/object.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080024#include "net/queue.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040025#include "chardev/char-fe.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080026#include "qemu/sockets.h"
Michael S. Tsirkinf27f01d2018-05-03 22:50:56 +030027#include "colo.h"
Wang Yongdd321ec2017-08-29 15:22:38 +080028#include "sysemu/iothread.h"
Zhang Chen0ffcece2018-09-03 12:38:43 +080029#include "net/colo-compare.h"
30#include "migration/colo.h"
Zhang Chendccd0312018-09-03 12:38:44 +080031#include "migration/migration.h"
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +040032#include "util.h"
Zhang Chen7dce4e62016-09-27 10:22:26 +080033
Lukas Straub9c55fe92020-05-22 15:53:53 +080034#include "block/aio-wait.h"
35#include "qemu/coroutine.h"
36
Zhang Chen7dce4e62016-09-27 10:22:26 +080037#define TYPE_COLO_COMPARE "colo-compare"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040038typedef struct CompareState CompareState;
Eduardo Habkost8110fa12020-08-31 17:07:33 -040039DECLARE_INSTANCE_CHECKER(CompareState, COLO_COMPARE,
40 TYPE_COLO_COMPARE)
Zhang Chen7dce4e62016-09-27 10:22:26 +080041
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 Chen2f2fcff2020-10-16 13:52:08 +080054#define REGULAR_PACKET_CHECK_MS 1000
Zhang Chen9cc43c92020-03-18 16:23:19 +080055#define DEFAULT_TIME_OUT_MS 3000
Zhang Chen0682e152016-09-27 10:22:30 +080056
Roman Bolshakov8c8ed032020-07-17 12:35:17 +030057/* #define DEBUG_COLO_PACKETS */
58
Lukas Straub45942b72020-05-22 15:53:55 +080059static QemuMutex colo_compare_mutex;
60static bool colo_compare_active;
Zhang Chen0ffcece2018-09-03 12:38:43 +080061static QemuMutex event_mtx;
62static QemuCond event_complete_cond;
63static int event_unhandled_count;
Zhang Chena2e5cb72020-06-24 09:20:41 +080064static uint32_t max_queue_size;
Zhang Chen0ffcece2018-09-03 12:38:43 +080065
Zhang Chen59509ec2016-09-27 10:22:27 +080066/*
Zhang Chen61c5f462017-09-05 14:31:06 +080067 * + CompareState ++
68 * | |
69 * +---------------+ +---------------+ +---------------+
70 * | conn list + - > conn + ------- > conn + -- > ......
71 * +---------------+ +---------------+ +---------------+
72 * | | | | | |
73 * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+
74 * |primary | |secondary |primary | |secondary
75 * |packet | |packet + |packet | |packet +
76 * +--------+ +--------+ +--------+ +--------+
77 * | | | |
78 * +---v----+ +---v----+ +---v----+ +---v----+
79 * |primary | |secondary |primary | |secondary
80 * |packet | |packet + |packet | |packet +
81 * +--------+ +--------+ +--------+ +--------+
82 * | | | |
83 * +---v----+ +---v----+ +---v----+ +---v----+
84 * |primary | |secondary |primary | |secondary
85 * |packet | |packet + |packet | |packet +
86 * +--------+ +--------+ +--------+ +--------+
87 */
Lukas Straub9c55fe92020-05-22 15:53:53 +080088
89typedef struct SendCo {
90 Coroutine *co;
91 struct CompareState *s;
92 CharBackend *chr;
93 GQueue send_list;
94 bool notify_remote_frame;
95 bool done;
96 int ret;
97} SendCo;
98
99typedef struct SendEntry {
100 uint32_t size;
101 uint32_t vnet_hdr_len;
102 uint8_t *buf;
103} SendEntry;
104
Eduardo Habkostdb1015e2020-09-03 16:43:22 -0400105struct CompareState {
Zhang Chen7dce4e62016-09-27 10:22:26 +0800106 Object parent;
107
108 char *pri_indev;
109 char *sec_indev;
110 char *outdev;
Zhang Chencf6af762019-06-10 00:44:29 +0800111 char *notify_dev;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300112 CharBackend chr_pri_in;
113 CharBackend chr_sec_in;
114 CharBackend chr_out;
Zhang Chen13025fe2019-06-10 00:44:30 +0800115 CharBackend chr_notify_dev;
Zhang Chen7dce4e62016-09-27 10:22:26 +0800116 SocketReadState pri_rs;
117 SocketReadState sec_rs;
Zhang Chen13025fe2019-06-10 00:44:30 +0800118 SocketReadState notify_rs;
Lukas Straub9c55fe92020-05-22 15:53:53 +0800119 SendCo out_sendco;
120 SendCo notify_sendco;
Zhang Chenaa3a7032017-07-04 14:53:52 +0800121 bool vnet_hdr;
Zhang Chen0c4266e2020-10-16 13:52:05 +0800122 uint64_t compare_timeout;
Zhang Chencca35ac2020-03-18 16:23:20 +0800123 uint32_t expired_scan_cycle;
Zhang Chen59509ec2016-09-27 10:22:27 +0800124
Zhang Chen61c5f462017-09-05 14:31:06 +0800125 /*
126 * Record the connection that through the NIC
127 * Element type: Connection
Zhang Chenb6540d42016-09-27 10:22:29 +0800128 */
129 GQueue conn_list;
Zhang Chen61c5f462017-09-05 14:31:06 +0800130 /* Record the connection without repetition */
Zhang Chen59509ec2016-09-27 10:22:27 +0800131 GHashTable *connection_track_table;
zhanghailiangdfd917a2017-02-17 10:53:12 +0800132
Wang Yongdd321ec2017-08-29 15:22:38 +0800133 IOThread *iothread;
zhanghailiangb43decb2017-02-17 10:53:14 +0800134 GMainContext *worker_context;
Wang Yongdd321ec2017-08-29 15:22:38 +0800135 QEMUTimer *packet_check_timer;
Zhang Chen0ffcece2018-09-03 12:38:43 +0800136
137 QEMUBH *event_bh;
138 enum colo_event event;
139
140 QTAILQ_ENTRY(CompareState) next;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -0400141};
Zhang Chen7dce4e62016-09-27 10:22:26 +0800142
143typedef struct CompareClass {
144 ObjectClass parent_class;
145} CompareClass;
146
Zhang Chen59509ec2016-09-27 10:22:27 +0800147enum {
148 PRIMARY_IN = 0,
149 SECONDARY_IN,
150};
151
Derek Subdadbb02020-05-22 15:53:57 +0800152static const char *colo_mode[] = {
153 [PRIMARY_IN] = "primary",
154 [SECONDARY_IN] = "secondary",
155};
Zhang Chen24525e92018-09-03 12:38:57 +0800156
Zhang Chen3037e7a2017-07-04 14:53:51 +0800157static int compare_chr_send(CompareState *s,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800158 uint8_t *buf,
Zhang Chenaa3a7032017-07-04 14:53:52 +0800159 uint32_t size,
Zhang Chen30685c02019-06-10 00:44:31 +0800160 uint32_t vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800161 bool notify_remote_frame,
162 bool zero_copy);
Zhang Chen59509ec2016-09-27 10:22:27 +0800163
Zhang Chenf77bed12019-07-23 01:18:28 +0800164static bool packet_matches_str(const char *str,
165 const uint8_t *buf,
166 uint32_t packet_len)
167{
168 if (packet_len != strlen(str)) {
169 return false;
170 }
171
Rao, Leiae4c2092021-11-01 15:56:59 +0800172 return !memcmp(str, buf, packet_len);
Zhang Chenf77bed12019-07-23 01:18:28 +0800173}
174
Zhang Chen1d09f702019-06-10 00:44:32 +0800175static void notify_remote_frame(CompareState *s)
176{
177 char msg[] = "DO_CHECKPOINT";
178 int ret = 0;
179
Lukas Straub9c55fe92020-05-22 15:53:53 +0800180 ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false);
Zhang Chen1d09f702019-06-10 00:44:32 +0800181 if (ret < 0) {
182 error_report("Notify Xen COLO-frame failed");
183 }
184}
185
186static void colo_compare_inconsistency_notify(CompareState *s)
187{
188 if (s->notify_dev) {
189 notify_remote_frame(s);
190 } else {
191 notifier_list_notify(&colo_compare_notifiers,
192 migrate_get_current());
193 }
194}
195
Rao, Lei33609e92020-10-16 13:52:00 +0800196/* Use restricted to colo_insert_packet() */
Zhang Chena935cc32017-01-24 16:53:46 +0800197static gint seq_sorter(Packet *a, Packet *b, gpointer data)
198{
Zhang Chena5f038e2022-01-14 13:09:00 +0800199 return b->tcp_seq - a->tcp_seq;
Zhang Chena935cc32017-01-24 16:53:46 +0800200}
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);
Zhang Chenfb5eca42021-11-18 11:20:10 +0800211 /* Need to consider ACK will bigger than uint32_t MAX */
212 *max_ack = pkt->tcp_ack - *max_ack > 0 ? pkt->tcp_ack : *max_ack;
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800213 pkt->header_size = pkt->transport_header - (uint8_t *)pkt->data
Rao, Lei5a2d9922021-06-08 16:23:31 +0800214 + (tcphd->th_off << 2);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800215 pkt->payload_size = pkt->size - pkt->header_size;
216 pkt->seq_end = pkt->tcp_seq + pkt->payload_size;
217 pkt->flags = tcphd->th_flags;
218}
219
Zhang Chen59509ec2016-09-27 10:22:27 +0800220/*
Mao Zhongyi8850d4c2017-10-13 14:32:06 +0800221 * Return 1 on success, if return 0 means the
222 * packet will be dropped
223 */
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800224static int colo_insert_packet(GQueue *queue, Packet *pkt, uint32_t *max_ack)
Mao Zhongyi8850d4c2017-10-13 14:32:06 +0800225{
Zhang Chena2e5cb72020-06-24 09:20:41 +0800226 if (g_queue_get_length(queue) <= max_queue_size) {
Mao Zhongyi8850d4c2017-10-13 14:32:06 +0800227 if (pkt->ip->ip_p == IPPROTO_TCP) {
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800228 fill_pkt_tcp_info(pkt, max_ack);
Mao Zhongyi8850d4c2017-10-13 14:32:06 +0800229 g_queue_insert_sorted(queue,
230 pkt,
231 (GCompareDataFunc)seq_sorter,
232 NULL);
233 } else {
234 g_queue_push_tail(queue, pkt);
235 }
236 return 1;
237 }
238 return 0;
239}
240
241/*
Zhang Chen59509ec2016-09-27 10:22:27 +0800242 * Return 0 on success, if return -1 means the pkt
243 * is unsupported(arp and ipv6) and will be sent later
244 */
Mao Zhongyi8ec14402017-10-13 14:32:07 +0800245static int packet_enqueue(CompareState *s, int mode, Connection **con)
Zhang Chen59509ec2016-09-27 10:22:27 +0800246{
Zhang Chenb6540d42016-09-27 10:22:29 +0800247 ConnectionKey key;
Zhang Chen59509ec2016-09-27 10:22:27 +0800248 Packet *pkt = NULL;
Zhang Chenb6540d42016-09-27 10:22:29 +0800249 Connection *conn;
Derek Subdadbb02020-05-22 15:53:57 +0800250 int ret;
Zhang Chen59509ec2016-09-27 10:22:27 +0800251
252 if (mode == PRIMARY_IN) {
Zhang Chenada1a332017-07-04 14:53:50 +0800253 pkt = packet_new(s->pri_rs.buf,
254 s->pri_rs.packet_len,
255 s->pri_rs.vnet_hdr_len);
Zhang Chen59509ec2016-09-27 10:22:27 +0800256 } else {
Zhang Chenada1a332017-07-04 14:53:50 +0800257 pkt = packet_new(s->sec_rs.buf,
258 s->sec_rs.packet_len,
259 s->sec_rs.vnet_hdr_len);
Zhang Chen59509ec2016-09-27 10:22:27 +0800260 }
261
262 if (parse_packet_early(pkt)) {
263 packet_destroy(pkt, NULL);
264 pkt = NULL;
265 return -1;
266 }
Rao, Lei64153ca2021-11-03 10:21:12 +0800267 fill_connection_key(pkt, &key, false);
Zhang Chen59509ec2016-09-27 10:22:27 +0800268
Zhang Chenb6540d42016-09-27 10:22:29 +0800269 conn = connection_get(s->connection_track_table,
270 &key,
271 &s->conn_list);
Zhang Chen59509ec2016-09-27 10:22:27 +0800272
Zhang Chenb6540d42016-09-27 10:22:29 +0800273 if (!conn->processing) {
274 g_queue_push_tail(&s->conn_list, conn);
275 conn->processing = true;
276 }
277
278 if (mode == PRIMARY_IN) {
Derek Subdadbb02020-05-22 15:53:57 +0800279 ret = colo_insert_packet(&conn->primary_list, pkt, &conn->pack);
Zhang Chenb6540d42016-09-27 10:22:29 +0800280 } else {
Derek Subdadbb02020-05-22 15:53:57 +0800281 ret = colo_insert_packet(&conn->secondary_list, pkt, &conn->sack);
Zhang Chenb6540d42016-09-27 10:22:29 +0800282 }
Derek Subdadbb02020-05-22 15:53:57 +0800283
284 if (!ret) {
285 trace_colo_compare_drop_packet(colo_mode[mode],
286 "queue size too big, drop packet");
287 packet_destroy(pkt, NULL);
288 pkt = NULL;
289 }
290
Mao Zhongyi4d366232017-11-16 10:28:32 +0800291 *con = conn;
Zhang Chen59509ec2016-09-27 10:22:27 +0800292
293 return 0;
294}
295
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800296static inline bool after(uint32_t seq1, uint32_t seq2)
297{
298 return (int32_t)(seq1 - seq2) > 0;
299}
300
301static void colo_release_primary_pkt(CompareState *s, Packet *pkt)
302{
303 int ret;
304 ret = compare_chr_send(s,
305 pkt->data,
306 pkt->size,
Zhang Chen30685c02019-06-10 00:44:31 +0800307 pkt->vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800308 false,
309 true);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800310 if (ret < 0) {
311 error_report("colo send primary packet failed");
312 }
313 trace_colo_compare_main("packet same and release packet");
Lukas Straub9c55fe92020-05-22 15:53:53 +0800314 packet_destroy_partial(pkt, NULL);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800315}
316
Zhang Chen0682e152016-09-27 10:22:30 +0800317/*
318 * The IP packets sent by primary and secondary
319 * will be compared in here
320 * TODO support ip fragment, Out-Of-Order
321 * return: 0 means packet same
322 * > 0 || < 0 means packet different
323 */
Mao Zhongyi93941332017-12-25 10:54:11 +0800324static int colo_compare_packet_payload(Packet *ppkt,
325 Packet *spkt,
326 uint16_t poffset,
327 uint16_t soffset,
328 uint16_t len)
329
Zhang Chen0682e152016-09-27 10:22:30 +0800330{
Roman Bolshakov8c8ed032020-07-17 12:35:17 +0300331 if (trace_event_get_state_backends(TRACE_COLO_COMPARE_IP_INFO)) {
Zhang Chene630b2b2017-03-09 15:40:07 +0800332 char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20];
333
334 strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src));
335 strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst));
336 strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src));
337 strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst));
338
339 trace_colo_compare_ip_info(ppkt->size, pri_ip_src,
340 pri_ip_dst, spkt->size,
341 sec_ip_src, sec_ip_dst);
342 }
Zhang Chen0682e152016-09-27 10:22:30 +0800343
Mao Zhongyi93941332017-12-25 10:54:11 +0800344 return memcmp(ppkt->data + poffset, spkt->data + soffset, len);
Zhang Chen0682e152016-09-27 10:22:30 +0800345}
346
Zhang Chenf4b61832016-09-27 10:22:31 +0800347/*
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800348 * return true means that the payload is consist and
349 * need to make the next comparison, false means do
350 * the checkpoint
351*/
352static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt,
353 int8_t *mark, uint32_t max_ack)
Zhang Chen0682e152016-09-27 10:22:30 +0800354{
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800355 *mark = 0;
Zhang Chenf4b61832016-09-27 10:22:31 +0800356
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800357 if (ppkt->tcp_seq == spkt->tcp_seq && ppkt->seq_end == spkt->seq_end) {
Fan Yang1e907a32019-09-24 22:08:29 +0800358 if (!colo_compare_packet_payload(ppkt, spkt,
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800359 ppkt->header_size, spkt->header_size,
360 ppkt->payload_size)) {
361 *mark = COLO_COMPARE_FREE_SECONDARY | COLO_COMPARE_FREE_PRIMARY;
362 return true;
363 }
364 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800365
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800366 /* one part of secondary packet payload still need to be compared */
367 if (!after(ppkt->seq_end, spkt->seq_end)) {
Fan Yang1e907a32019-09-24 22:08:29 +0800368 if (!colo_compare_packet_payload(ppkt, spkt,
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800369 ppkt->header_size + ppkt->offset,
370 spkt->header_size + spkt->offset,
371 ppkt->payload_size - ppkt->offset)) {
372 if (!after(ppkt->tcp_ack, max_ack)) {
373 *mark = COLO_COMPARE_FREE_PRIMARY;
374 spkt->offset += ppkt->payload_size - ppkt->offset;
375 return true;
376 } else {
377 /* secondary guest hasn't ack the data, don't send
378 * out this packet
379 */
380 return false;
381 }
382 }
Zhang Chen6efeb322017-03-02 17:54:17 +0800383 } else {
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800384 /* primary packet is longer than secondary packet, compare
385 * the same part and mark the primary packet offset
386 */
Fan Yang1e907a32019-09-24 22:08:29 +0800387 if (!colo_compare_packet_payload(ppkt, spkt,
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800388 ppkt->header_size + ppkt->offset,
389 spkt->header_size + spkt->offset,
390 spkt->payload_size - spkt->offset)) {
391 *mark = COLO_COMPARE_FREE_SECONDARY;
392 ppkt->offset += spkt->payload_size - spkt->offset;
393 return true;
394 }
Zhang Chen6efeb322017-03-02 17:54:17 +0800395 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800396
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800397 return false;
398}
Alex Bennée2dfe5112016-10-28 14:25:59 +0100399
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800400static void colo_compare_tcp(CompareState *s, Connection *conn)
401{
402 Packet *ppkt = NULL, *spkt = NULL;
403 int8_t mark;
Zhang Chenf583dca2017-04-27 11:46:45 +0800404
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800405 /*
406 * If ppkt and spkt have the same payload, but ppkt's ACK
407 * is greater than spkt's ACK, in this case we can not
408 * send the ppkt because it will cause the secondary guest
409 * to miss sending some data in the next. Therefore, we
410 * record the maximum ACK in the current queue at both
411 * primary side and secondary side. Only when the ack is
412 * less than the smaller of the two maximum ack, then we
413 * can ensure that the packet's payload is acknowledged by
414 * primary and secondary.
415 */
Zhang Chenfb5eca42021-11-18 11:20:10 +0800416 uint32_t min_ack = conn->pack - conn->sack > 0 ?
417 conn->sack : conn->pack;
Zhang Chenf583dca2017-04-27 11:46:45 +0800418
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800419pri:
420 if (g_queue_is_empty(&conn->primary_list)) {
421 return;
422 }
Zhang Chena5f038e2022-01-14 13:09:00 +0800423 ppkt = g_queue_pop_tail(&conn->primary_list);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800424sec:
425 if (g_queue_is_empty(&conn->secondary_list)) {
Zhang Chena5f038e2022-01-14 13:09:00 +0800426 g_queue_push_tail(&conn->primary_list, ppkt);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800427 return;
428 }
Zhang Chena5f038e2022-01-14 13:09:00 +0800429 spkt = g_queue_pop_tail(&conn->secondary_list);
Zhang Chenf583dca2017-04-27 11:46:45 +0800430
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800431 if (ppkt->tcp_seq == ppkt->seq_end) {
432 colo_release_primary_pkt(s, ppkt);
433 ppkt = NULL;
434 }
435
436 if (ppkt && conn->compare_seq && !after(ppkt->seq_end, conn->compare_seq)) {
437 trace_colo_compare_main("pri: this packet has compared");
438 colo_release_primary_pkt(s, ppkt);
439 ppkt = NULL;
440 }
441
442 if (spkt->tcp_seq == spkt->seq_end) {
443 packet_destroy(spkt, NULL);
444 if (!ppkt) {
445 goto pri;
446 } else {
447 goto sec;
448 }
449 } else {
450 if (conn->compare_seq && !after(spkt->seq_end, conn->compare_seq)) {
451 trace_colo_compare_main("sec: this packet has compared");
452 packet_destroy(spkt, NULL);
453 if (!ppkt) {
454 goto pri;
455 } else {
456 goto sec;
457 }
458 }
459 if (!ppkt) {
Zhang Chena5f038e2022-01-14 13:09:00 +0800460 g_queue_push_tail(&conn->secondary_list, spkt);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800461 goto pri;
462 }
463 }
464
465 if (colo_mark_tcp_pkt(ppkt, spkt, &mark, min_ack)) {
466 trace_colo_compare_tcp_info("pri",
467 ppkt->tcp_seq, ppkt->tcp_ack,
468 ppkt->header_size, ppkt->payload_size,
469 ppkt->offset, ppkt->flags);
470
471 trace_colo_compare_tcp_info("sec",
472 spkt->tcp_seq, spkt->tcp_ack,
473 spkt->header_size, spkt->payload_size,
474 spkt->offset, spkt->flags);
475
476 if (mark == COLO_COMPARE_FREE_PRIMARY) {
477 conn->compare_seq = ppkt->seq_end;
478 colo_release_primary_pkt(s, ppkt);
Zhang Chena5f038e2022-01-14 13:09:00 +0800479 g_queue_push_tail(&conn->secondary_list, spkt);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800480 goto pri;
Li Zhijian45b9e8c2020-10-16 13:52:04 +0800481 } else if (mark == COLO_COMPARE_FREE_SECONDARY) {
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800482 conn->compare_seq = spkt->seq_end;
483 packet_destroy(spkt, NULL);
484 goto sec;
Li Zhijian45b9e8c2020-10-16 13:52:04 +0800485 } else if (mark == (COLO_COMPARE_FREE_PRIMARY | COLO_COMPARE_FREE_SECONDARY)) {
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800486 conn->compare_seq = ppkt->seq_end;
487 colo_release_primary_pkt(s, ppkt);
488 packet_destroy(spkt, NULL);
489 goto pri;
490 }
491 } else {
Zhang Chena5f038e2022-01-14 13:09:00 +0800492 g_queue_push_tail(&conn->primary_list, ppkt);
493 g_queue_push_tail(&conn->secondary_list, spkt);
Zhang Chenf4b61832016-09-27 10:22:31 +0800494
Roman Bolshakov8c8ed032020-07-17 12:35:17 +0300495#ifdef DEBUG_COLO_PACKETS
Peter Maydell842038f2020-09-12 14:23:15 +0100496 qemu_hexdump(stderr, "colo-compare ppkt", ppkt->data, ppkt->size);
497 qemu_hexdump(stderr, "colo-compare spkt", spkt->data, spkt->size);
Roman Bolshakov8c8ed032020-07-17 12:35:17 +0300498#endif
Zhang Chenf4b61832016-09-27 10:22:31 +0800499
Zhang Chen1d09f702019-06-10 00:44:32 +0800500 colo_compare_inconsistency_notify(s);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800501 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800502}
503
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800504
Zhang Chenf4b61832016-09-27 10:22:31 +0800505/*
506 * Called from the compare thread on the primary
507 * for compare udp packet
508 */
509static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
510{
Mao Zhongyi93941332017-12-25 10:54:11 +0800511 uint16_t network_header_length = ppkt->ip->ip_hl << 2;
512 uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len;
Zhang Chenf4b61832016-09-27 10:22:31 +0800513
514 trace_colo_compare_main("compare udp");
Zhang Chen2ad7ca42017-03-02 17:54:16 +0800515
Zhang Chen6efeb322017-03-02 17:54:17 +0800516 /*
517 * Because of ppkt and spkt are both in the same connection,
518 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
519 * same with spkt. In addition, IP header's Identification is a random
520 * field, we can handle it in IP fragmentation function later.
521 * COLO just concern the response net packet payload from primary guest
522 * and secondary guest are same or not, So we ignored all IP header include
523 * other field like TOS,TTL,IP Checksum. we only need to compare
524 * the ip payload here.
525 */
Mao Zhongyi93941332017-12-25 10:54:11 +0800526 if (ppkt->size != spkt->size) {
527 trace_colo_compare_main("UDP: payload size of packets are different");
528 return -1;
529 }
530 if (colo_compare_packet_payload(ppkt, spkt, offset, offset,
531 ppkt->size - offset)) {
Zhang Chenf4b61832016-09-27 10:22:31 +0800532 trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
Zhang Chenf4b61832016-09-27 10:22:31 +0800533 trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
Roman Bolshakov8c8ed032020-07-17 12:35:17 +0300534#ifdef DEBUG_COLO_PACKETS
Peter Maydell842038f2020-09-12 14:23:15 +0100535 qemu_hexdump(stderr, "colo-compare pri pkt", ppkt->data, ppkt->size);
536 qemu_hexdump(stderr, "colo-compare sec pkt", spkt->data, spkt->size);
Roman Bolshakov8c8ed032020-07-17 12:35:17 +0300537#endif
Mao Zhongyi93941332017-12-25 10:54:11 +0800538 return -1;
539 } else {
540 return 0;
Zhang Chenf4b61832016-09-27 10:22:31 +0800541 }
Zhang Chenf4b61832016-09-27 10:22:31 +0800542}
543
544/*
545 * Called from the compare thread on the primary
546 * for compare icmp packet
547 */
548static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
549{
Mao Zhongyi93941332017-12-25 10:54:11 +0800550 uint16_t network_header_length = ppkt->ip->ip_hl << 2;
551 uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len;
Zhang Chen6efeb322017-03-02 17:54:17 +0800552
Zhang Chenf4b61832016-09-27 10:22:31 +0800553 trace_colo_compare_main("compare icmp");
Zhang Chenf4b61832016-09-27 10:22:31 +0800554
Zhang Chen6efeb322017-03-02 17:54:17 +0800555 /*
556 * Because of ppkt and spkt are both in the same connection,
557 * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are
558 * same with spkt. In addition, IP header's Identification is a random
559 * field, we can handle it in IP fragmentation function later.
560 * COLO just concern the response net packet payload from primary guest
561 * and secondary guest are same or not, So we ignored all IP header include
562 * other field like TOS,TTL,IP Checksum. we only need to compare
563 * the ip payload here.
564 */
Mao Zhongyi93941332017-12-25 10:54:11 +0800565 if (ppkt->size != spkt->size) {
566 trace_colo_compare_main("ICMP: payload size of packets are different");
567 return -1;
568 }
569 if (colo_compare_packet_payload(ppkt, spkt, offset, offset,
570 ppkt->size - offset)) {
Zhang Chenf4b61832016-09-27 10:22:31 +0800571 trace_colo_compare_icmp_miscompare("primary pkt size",
572 ppkt->size);
Zhang Chenf4b61832016-09-27 10:22:31 +0800573 trace_colo_compare_icmp_miscompare("Secondary pkt size",
574 spkt->size);
Roman Bolshakov8c8ed032020-07-17 12:35:17 +0300575#ifdef DEBUG_COLO_PACKETS
Peter Maydell842038f2020-09-12 14:23:15 +0100576 qemu_hexdump(stderr, "colo-compare pri pkt", ppkt->data, ppkt->size);
577 qemu_hexdump(stderr, "colo-compare sec pkt", spkt->data, spkt->size);
Roman Bolshakov8c8ed032020-07-17 12:35:17 +0300578#endif
Zhang Chenf4b61832016-09-27 10:22:31 +0800579 return -1;
580 } else {
581 return 0;
582 }
583}
584
585/*
586 * Called from the compare thread on the primary
587 * for compare other packet
588 */
589static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
590{
Mao Zhongyi93941332017-12-25 10:54:11 +0800591 uint16_t offset = ppkt->vnet_hdr_len;
592
Zhang Chenf4b61832016-09-27 10:22:31 +0800593 trace_colo_compare_main("compare other");
Mao Zhongyi93941332017-12-25 10:54:11 +0800594 if (ppkt->size != spkt->size) {
595 trace_colo_compare_main("Other: payload size of packets are different");
596 return -1;
597 }
598 return colo_compare_packet_payload(ppkt, spkt, offset, offset,
599 ppkt->size - offset);
Zhang Chen0682e152016-09-27 10:22:30 +0800600}
601
602static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
603{
604 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST);
605
606 if ((now - pkt->creation_ms) > (*check_time)) {
607 trace_colo_old_packet_check_found(pkt->creation_ms);
608 return 0;
609 } else {
610 return 1;
611 }
612}
613
Zhang Chendccd0312018-09-03 12:38:44 +0800614void colo_compare_register_notifier(Notifier *notify)
615{
616 notifier_list_add(&colo_compare_notifiers, notify);
617}
618
619void colo_compare_unregister_notifier(Notifier *notify)
620{
621 notifier_remove(notify);
622}
623
Zhang Chend25a7da2017-04-12 20:12:07 +0800624static int colo_old_packet_check_one_conn(Connection *conn,
Zhang Chen1d09f702019-06-10 00:44:32 +0800625 CompareState *s)
Zhang Chen0682e152016-09-27 10:22:30 +0800626{
Zhang Chen17475df2020-10-16 13:52:07 +0800627 if (!g_queue_is_empty(&conn->primary_list)) {
628 if (g_queue_find_custom(&conn->primary_list,
629 &s->compare_timeout,
630 (GCompareFunc)colo_old_packet_check_one))
631 goto out;
632 }
Zhang Chen0682e152016-09-27 10:22:30 +0800633
Zhang Chen17475df2020-10-16 13:52:07 +0800634 if (!g_queue_is_empty(&conn->secondary_list)) {
635 if (g_queue_find_custom(&conn->secondary_list,
636 &s->compare_timeout,
637 (GCompareFunc)colo_old_packet_check_one))
638 goto out;
Zhang Chen0682e152016-09-27 10:22:30 +0800639 }
Zhang Chend25a7da2017-04-12 20:12:07 +0800640
641 return 1;
Zhang Chen17475df2020-10-16 13:52:07 +0800642
643out:
644 /* Do checkpoint will flush old packet */
645 colo_compare_inconsistency_notify(s);
646 return 0;
Zhang Chen0682e152016-09-27 10:22:30 +0800647}
648
649/*
650 * Look for old packets that the secondary hasn't matched,
651 * if we have some then we have to checkpoint to wake
652 * the secondary up.
653 */
654static void colo_old_packet_check(void *opaque)
655{
656 CompareState *s = opaque;
657
Zhang Chend25a7da2017-04-12 20:12:07 +0800658 /*
659 * If we find one old packet, stop finding job and notify
660 * COLO frame do checkpoint.
661 */
Zhang Chen1d09f702019-06-10 00:44:32 +0800662 g_queue_find_custom(&s->conn_list, s,
Zhang Chend25a7da2017-04-12 20:12:07 +0800663 (GCompareFunc)colo_old_packet_check_one_conn);
Zhang Chen0682e152016-09-27 10:22:30 +0800664}
665
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800666static void colo_compare_packet(CompareState *s, Connection *conn,
667 int (*HandlePacket)(Packet *spkt,
668 Packet *ppkt))
Zhang Chen0682e152016-09-27 10:22:30 +0800669{
Zhang Chen0682e152016-09-27 10:22:30 +0800670 Packet *pkt = NULL;
671 GList *result = NULL;
Zhang Chen0682e152016-09-27 10:22:30 +0800672
673 while (!g_queue_is_empty(&conn->primary_list) &&
674 !g_queue_is_empty(&conn->secondary_list)) {
Zhang Chena5f038e2022-01-14 13:09:00 +0800675 pkt = g_queue_pop_tail(&conn->primary_list);
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800676 result = g_queue_find_custom(&conn->secondary_list,
677 pkt, (GCompareFunc)HandlePacket);
Zhang Chen0682e152016-09-27 10:22:30 +0800678
679 if (result) {
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800680 colo_release_primary_pkt(s, pkt);
Lukas Straub9162ed62021-03-18 12:11:51 +0800681 packet_destroy(result->data, NULL);
Lukas Straub739128e2021-03-18 12:11:52 +0800682 g_queue_delete_link(&conn->secondary_list, result);
Zhang Chen0682e152016-09-27 10:22:30 +0800683 } else {
684 /*
685 * If one packet arrive late, the secondary_list or
686 * primary_list will be empty, so we can't compare it
Zhang Chendccd0312018-09-03 12:38:44 +0800687 * until next comparison. If the packets in the list are
688 * timeout, it will trigger a checkpoint request.
Zhang Chen0682e152016-09-27 10:22:30 +0800689 */
690 trace_colo_compare_main("packet different");
Zhang Chena5f038e2022-01-14 13:09:00 +0800691 g_queue_push_tail(&conn->primary_list, pkt);
Zhang Chen1d09f702019-06-10 00:44:32 +0800692
693 colo_compare_inconsistency_notify(s);
Zhang Chen0682e152016-09-27 10:22:30 +0800694 break;
695 }
696 }
697}
698
Mao Zhongyif449c9e2017-12-25 10:54:12 +0800699/*
700 * Called from the compare thread on the primary
701 * for compare packet with secondary list of the
702 * specified connection when a new packet was
703 * queued to it.
704 */
705static void colo_compare_connection(void *opaque, void *user_data)
706{
707 CompareState *s = user_data;
708 Connection *conn = opaque;
709
710 switch (conn->ip_proto) {
711 case IPPROTO_TCP:
712 colo_compare_tcp(s, conn);
713 break;
714 case IPPROTO_UDP:
715 colo_compare_packet(s, conn, colo_packet_compare_udp);
716 break;
717 case IPPROTO_ICMP:
718 colo_compare_packet(s, conn, colo_packet_compare_icmp);
719 break;
720 default:
721 colo_compare_packet(s, conn, colo_packet_compare_other);
722 break;
723 }
724}
725
Lukas Straub9c55fe92020-05-22 15:53:53 +0800726static void coroutine_fn _compare_chr_send(void *opaque)
727{
728 SendCo *sendco = opaque;
729 CompareState *s = sendco->s;
730 int ret = 0;
731
732 while (!g_queue_is_empty(&sendco->send_list)) {
733 SendEntry *entry = g_queue_pop_tail(&sendco->send_list);
734 uint32_t len = htonl(entry->size);
735
736 ret = qemu_chr_fe_write_all(sendco->chr, (uint8_t *)&len, sizeof(len));
737
738 if (ret != sizeof(len)) {
739 g_free(entry->buf);
740 g_slice_free(SendEntry, entry);
741 goto err;
742 }
743
744 if (!sendco->notify_remote_frame && s->vnet_hdr) {
745 /*
746 * We send vnet header len make other module(like filter-redirector)
747 * know how to parse net packet correctly.
748 */
749 len = htonl(entry->vnet_hdr_len);
750
751 ret = qemu_chr_fe_write_all(sendco->chr,
752 (uint8_t *)&len,
753 sizeof(len));
754
755 if (ret != sizeof(len)) {
756 g_free(entry->buf);
757 g_slice_free(SendEntry, entry);
758 goto err;
759 }
760 }
761
762 ret = qemu_chr_fe_write_all(sendco->chr,
763 (uint8_t *)entry->buf,
764 entry->size);
765
766 if (ret != entry->size) {
767 g_free(entry->buf);
768 g_slice_free(SendEntry, entry);
769 goto err;
770 }
771
772 g_free(entry->buf);
773 g_slice_free(SendEntry, entry);
774 }
775
776 sendco->ret = 0;
777 goto out;
778
779err:
780 while (!g_queue_is_empty(&sendco->send_list)) {
781 SendEntry *entry = g_queue_pop_tail(&sendco->send_list);
782 g_free(entry->buf);
783 g_slice_free(SendEntry, entry);
784 }
785 sendco->ret = ret < 0 ? ret : -EIO;
786out:
787 sendco->co = NULL;
788 sendco->done = true;
789 aio_wait_kick();
790}
791
Zhang Chen3037e7a2017-07-04 14:53:51 +0800792static int compare_chr_send(CompareState *s,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800793 uint8_t *buf,
Zhang Chenaa3a7032017-07-04 14:53:52 +0800794 uint32_t size,
Zhang Chen30685c02019-06-10 00:44:31 +0800795 uint32_t vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +0800796 bool notify_remote_frame,
797 bool zero_copy)
Zhang Chen59509ec2016-09-27 10:22:27 +0800798{
Lukas Straub9c55fe92020-05-22 15:53:53 +0800799 SendCo *sendco;
800 SendEntry *entry;
801
802 if (notify_remote_frame) {
803 sendco = &s->notify_sendco;
804 } else {
805 sendco = &s->out_sendco;
806 }
Zhang Chen59509ec2016-09-27 10:22:27 +0800807
808 if (!size) {
Zhang Chen0656fbc2021-11-18 11:20:11 +0800809 return -1;
Zhang Chen59509ec2016-09-27 10:22:27 +0800810 }
811
Lukas Straub9c55fe92020-05-22 15:53:53 +0800812 entry = g_slice_new(SendEntry);
813 entry->size = size;
814 entry->vnet_hdr_len = vnet_hdr_len;
815 if (zero_copy) {
816 entry->buf = buf;
Zhang Chen30685c02019-06-10 00:44:31 +0800817 } else {
Lukas Straub9c55fe92020-05-22 15:53:53 +0800818 entry->buf = g_malloc(size);
819 memcpy(entry->buf, buf, size);
Zhang Chen30685c02019-06-10 00:44:31 +0800820 }
Zhang Chena5f038e2022-01-14 13:09:00 +0800821 g_queue_push_tail(&sendco->send_list, entry);
Zhang Chen30685c02019-06-10 00:44:31 +0800822
Lukas Straub9c55fe92020-05-22 15:53:53 +0800823 if (sendco->done) {
824 sendco->co = qemu_coroutine_create(_compare_chr_send, sendco);
825 sendco->done = false;
826 qemu_coroutine_enter(sendco->co);
827 if (sendco->done) {
828 /* report early errors */
829 return sendco->ret;
Zhang Chenaa3a7032017-07-04 14:53:52 +0800830 }
831 }
832
Lukas Straub9c55fe92020-05-22 15:53:53 +0800833 /* assume success */
Zhang Chen59509ec2016-09-27 10:22:27 +0800834 return 0;
Zhang Chen59509ec2016-09-27 10:22:27 +0800835}
836
Zhang Chen0682e152016-09-27 10:22:30 +0800837static int compare_chr_can_read(void *opaque)
838{
839 return COMPARE_READ_LEN_MAX;
840}
841
842/*
843 * Called from the main thread on the primary for packets
844 * arriving over the socket from the primary.
845 */
846static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
847{
848 CompareState *s = COLO_COMPARE(opaque);
849 int ret;
850
851 ret = net_fill_rstate(&s->pri_rs, buf, size);
852 if (ret == -1) {
Anton Nefedov81517ba2017-07-06 15:08:49 +0300853 qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +0300854 NULL, NULL, true);
Zhang Chen0682e152016-09-27 10:22:30 +0800855 error_report("colo-compare primary_in error");
856 }
857}
858
859/*
860 * Called from the main thread on the primary for packets
861 * arriving over the socket from the secondary.
862 */
863static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
864{
865 CompareState *s = COLO_COMPARE(opaque);
866 int ret;
867
868 ret = net_fill_rstate(&s->sec_rs, buf, size);
869 if (ret == -1) {
Anton Nefedov81517ba2017-07-06 15:08:49 +0300870 qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +0300871 NULL, NULL, true);
Zhang Chen0682e152016-09-27 10:22:30 +0800872 error_report("colo-compare secondary_in error");
873 }
874}
875
Zhang Chen13025fe2019-06-10 00:44:30 +0800876static void compare_notify_chr(void *opaque, const uint8_t *buf, int size)
877{
878 CompareState *s = COLO_COMPARE(opaque);
879 int ret;
880
881 ret = net_fill_rstate(&s->notify_rs, buf, size);
882 if (ret == -1) {
883 qemu_chr_fe_set_handlers(&s->chr_notify_dev, NULL, NULL, NULL, NULL,
884 NULL, NULL, true);
885 error_report("colo-compare notify_dev error");
886 }
887}
888
zhanghailiang66d2a242017-02-17 10:53:11 +0800889/*
890 * Check old packet regularly so it can watch for any packets
891 * that the secondary hasn't produced equivalents of.
892 */
Wang Yongdd321ec2017-08-29 15:22:38 +0800893static void check_old_packet_regular(void *opaque)
zhanghailiang66d2a242017-02-17 10:53:11 +0800894{
895 CompareState *s = opaque;
896
897 /* if have old packet we will notify checkpoint */
898 colo_old_packet_check(s);
Zhang Chenec081982020-10-16 13:52:06 +0800899 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_HOST) +
Zhang Chencca35ac2020-03-18 16:23:20 +0800900 s->expired_scan_cycle);
zhanghailiang66d2a242017-02-17 10:53:11 +0800901}
902
Zhang Chen0ffcece2018-09-03 12:38:43 +0800903/* Public API, Used for COLO frame to notify compare event */
904void colo_notify_compares_event(void *opaque, int event, Error **errp)
905{
906 CompareState *s;
Lukas Straub45942b72020-05-22 15:53:55 +0800907 qemu_mutex_lock(&colo_compare_mutex);
908
909 if (!colo_compare_active) {
910 qemu_mutex_unlock(&colo_compare_mutex);
911 return;
912 }
Zhang Chen0ffcece2018-09-03 12:38:43 +0800913
914 qemu_mutex_lock(&event_mtx);
915 QTAILQ_FOREACH(s, &net_compares, next) {
916 s->event = event;
917 qemu_bh_schedule(s->event_bh);
918 event_unhandled_count++;
919 }
920 /* Wait all compare threads to finish handling this event */
921 while (event_unhandled_count > 0) {
922 qemu_cond_wait(&event_complete_cond, &event_mtx);
923 }
924
925 qemu_mutex_unlock(&event_mtx);
Lukas Straub45942b72020-05-22 15:53:55 +0800926 qemu_mutex_unlock(&colo_compare_mutex);
Zhang Chen0ffcece2018-09-03 12:38:43 +0800927}
928
Wang Yongdd321ec2017-08-29 15:22:38 +0800929static void colo_compare_timer_init(CompareState *s)
Zhang Chen0682e152016-09-27 10:22:30 +0800930{
Wang Yongdd321ec2017-08-29 15:22:38 +0800931 AioContext *ctx = iothread_get_aio_context(s->iothread);
Zhang Chen0682e152016-09-27 10:22:30 +0800932
Zhang Chenec081982020-10-16 13:52:06 +0800933 s->packet_check_timer = aio_timer_new(ctx, QEMU_CLOCK_HOST,
Wang Yongdd321ec2017-08-29 15:22:38 +0800934 SCALE_MS, check_old_packet_regular,
935 s);
Zhang Chenec081982020-10-16 13:52:06 +0800936 timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_HOST) +
Zhang Chencca35ac2020-03-18 16:23:20 +0800937 s->expired_scan_cycle);
Wang Yongdd321ec2017-08-29 15:22:38 +0800938}
939
940static void colo_compare_timer_del(CompareState *s)
941{
942 if (s->packet_check_timer) {
Wang Yongdd321ec2017-08-29 15:22:38 +0800943 timer_free(s->packet_check_timer);
944 s->packet_check_timer = NULL;
945 }
946 }
947
Zhang Chen0ffcece2018-09-03 12:38:43 +0800948static void colo_flush_packets(void *opaque, void *user_data);
949
950static void colo_compare_handle_event(void *opaque)
951{
952 CompareState *s = opaque;
953
954 switch (s->event) {
955 case COLO_EVENT_CHECKPOINT:
956 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
957 break;
958 case COLO_EVENT_FAILOVER:
959 break;
960 default:
961 break;
962 }
963
Zhang Chen0ffcece2018-09-03 12:38:43 +0800964 qemu_mutex_lock(&event_mtx);
Lukas Straub78e4f442019-04-20 19:14:25 +0200965 assert(event_unhandled_count > 0);
Zhang Chen0ffcece2018-09-03 12:38:43 +0800966 event_unhandled_count--;
967 qemu_cond_broadcast(&event_complete_cond);
968 qemu_mutex_unlock(&event_mtx);
969}
970
Wang Yongdd321ec2017-08-29 15:22:38 +0800971static void colo_compare_iothread(CompareState *s)
972{
Lukas Straub5893c732020-05-22 15:53:51 +0800973 AioContext *ctx = iothread_get_aio_context(s->iothread);
Wang Yongdd321ec2017-08-29 15:22:38 +0800974 object_ref(OBJECT(s->iothread));
975 s->worker_context = iothread_get_g_main_context(s->iothread);
Zhang Chen0682e152016-09-27 10:22:30 +0800976
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300977 qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300978 compare_pri_chr_in, NULL, NULL,
979 s, s->worker_context, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300980 qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300981 compare_sec_chr_in, NULL, NULL,
982 s, s->worker_context, true);
Zhang Chen13025fe2019-06-10 00:44:30 +0800983 if (s->notify_dev) {
984 qemu_chr_fe_set_handlers(&s->chr_notify_dev, compare_chr_can_read,
985 compare_notify_chr, NULL, NULL,
986 s, s->worker_context, true);
987 }
Zhang Chen0682e152016-09-27 10:22:30 +0800988
Wang Yongdd321ec2017-08-29 15:22:38 +0800989 colo_compare_timer_init(s);
Lukas Straub5893c732020-05-22 15:53:51 +0800990 s->event_bh = aio_bh_new(ctx, colo_compare_handle_event, s);
Zhang Chen0682e152016-09-27 10:22:30 +0800991}
992
Zhang Chen7dce4e62016-09-27 10:22:26 +0800993static char *compare_get_pri_indev(Object *obj, Error **errp)
994{
995 CompareState *s = COLO_COMPARE(obj);
996
997 return g_strdup(s->pri_indev);
998}
999
1000static void compare_set_pri_indev(Object *obj, const char *value, Error **errp)
1001{
1002 CompareState *s = COLO_COMPARE(obj);
1003
1004 g_free(s->pri_indev);
1005 s->pri_indev = g_strdup(value);
1006}
1007
1008static char *compare_get_sec_indev(Object *obj, Error **errp)
1009{
1010 CompareState *s = COLO_COMPARE(obj);
1011
1012 return g_strdup(s->sec_indev);
1013}
1014
1015static void compare_set_sec_indev(Object *obj, const char *value, Error **errp)
1016{
1017 CompareState *s = COLO_COMPARE(obj);
1018
1019 g_free(s->sec_indev);
1020 s->sec_indev = g_strdup(value);
1021}
1022
1023static char *compare_get_outdev(Object *obj, Error **errp)
1024{
1025 CompareState *s = COLO_COMPARE(obj);
1026
1027 return g_strdup(s->outdev);
1028}
1029
1030static void compare_set_outdev(Object *obj, const char *value, Error **errp)
1031{
1032 CompareState *s = COLO_COMPARE(obj);
1033
1034 g_free(s->outdev);
1035 s->outdev = g_strdup(value);
1036}
1037
Zhang Chenaa3a7032017-07-04 14:53:52 +08001038static bool compare_get_vnet_hdr(Object *obj, Error **errp)
1039{
1040 CompareState *s = COLO_COMPARE(obj);
1041
1042 return s->vnet_hdr;
1043}
1044
1045static void compare_set_vnet_hdr(Object *obj,
1046 bool value,
1047 Error **errp)
1048{
1049 CompareState *s = COLO_COMPARE(obj);
1050
1051 s->vnet_hdr = value;
1052}
1053
Zhang Chencf6af762019-06-10 00:44:29 +08001054static char *compare_get_notify_dev(Object *obj, Error **errp)
1055{
1056 CompareState *s = COLO_COMPARE(obj);
1057
1058 return g_strdup(s->notify_dev);
1059}
1060
1061static void compare_set_notify_dev(Object *obj, const char *value, Error **errp)
1062{
1063 CompareState *s = COLO_COMPARE(obj);
1064
1065 g_free(s->notify_dev);
1066 s->notify_dev = g_strdup(value);
1067}
1068
Zhang Chen9cc43c92020-03-18 16:23:19 +08001069static void compare_get_timeout(Object *obj, Visitor *v,
1070 const char *name, void *opaque,
1071 Error **errp)
1072{
1073 CompareState *s = COLO_COMPARE(obj);
Zhang Chen0c4266e2020-10-16 13:52:05 +08001074 uint64_t value = s->compare_timeout;
Zhang Chen9cc43c92020-03-18 16:23:19 +08001075
Zhang Chen0c4266e2020-10-16 13:52:05 +08001076 visit_type_uint64(v, name, &value, errp);
Zhang Chen9cc43c92020-03-18 16:23:19 +08001077}
1078
1079static void compare_set_timeout(Object *obj, Visitor *v,
1080 const char *name, void *opaque,
1081 Error **errp)
1082{
1083 CompareState *s = COLO_COMPARE(obj);
Zhang Chen9cc43c92020-03-18 16:23:19 +08001084 uint32_t value;
1085
Markus Armbruster668f62e2020-07-07 18:06:02 +02001086 if (!visit_type_uint32(v, name, &value, errp)) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +02001087 return;
Zhang Chen9cc43c92020-03-18 16:23:19 +08001088 }
1089 if (!value) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +02001090 error_setg(errp, "Property '%s.%s' requires a positive value",
Zhang Chen9cc43c92020-03-18 16:23:19 +08001091 object_get_typename(obj), name);
Markus Armbrusterdcfe4802020-07-07 18:06:01 +02001092 return;
Zhang Chen9cc43c92020-03-18 16:23:19 +08001093 }
1094 s->compare_timeout = value;
Zhang Chen9cc43c92020-03-18 16:23:19 +08001095}
1096
Zhang Chencca35ac2020-03-18 16:23:20 +08001097static void compare_get_expired_scan_cycle(Object *obj, Visitor *v,
1098 const char *name, void *opaque,
1099 Error **errp)
1100{
1101 CompareState *s = COLO_COMPARE(obj);
1102 uint32_t value = s->expired_scan_cycle;
1103
1104 visit_type_uint32(v, name, &value, errp);
1105}
1106
1107static void compare_set_expired_scan_cycle(Object *obj, Visitor *v,
1108 const char *name, void *opaque,
1109 Error **errp)
1110{
1111 CompareState *s = COLO_COMPARE(obj);
Zhang Chencca35ac2020-03-18 16:23:20 +08001112 uint32_t value;
1113
Markus Armbruster668f62e2020-07-07 18:06:02 +02001114 if (!visit_type_uint32(v, name, &value, errp)) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +02001115 return;
Zhang Chencca35ac2020-03-18 16:23:20 +08001116 }
1117 if (!value) {
Markus Armbrusterdcfe4802020-07-07 18:06:01 +02001118 error_setg(errp, "Property '%s.%s' requires a positive value",
Zhang Chencca35ac2020-03-18 16:23:20 +08001119 object_get_typename(obj), name);
Markus Armbrusterdcfe4802020-07-07 18:06:01 +02001120 return;
Zhang Chencca35ac2020-03-18 16:23:20 +08001121 }
1122 s->expired_scan_cycle = value;
Zhang Chencca35ac2020-03-18 16:23:20 +08001123}
1124
Zhang Chena2e5cb72020-06-24 09:20:41 +08001125static void get_max_queue_size(Object *obj, Visitor *v,
1126 const char *name, void *opaque,
1127 Error **errp)
1128{
1129 uint32_t value = max_queue_size;
1130
1131 visit_type_uint32(v, name, &value, errp);
1132}
1133
1134static void set_max_queue_size(Object *obj, Visitor *v,
1135 const char *name, void *opaque,
1136 Error **errp)
1137{
Zhang Chen0c4266e2020-10-16 13:52:05 +08001138 uint64_t value;
Zhang Chena2e5cb72020-06-24 09:20:41 +08001139
Markus Armbrusterd1c81c32022-11-21 09:50:53 +01001140 if (!visit_type_uint64(v, name, &value, errp)) {
1141 return;
Zhang Chena2e5cb72020-06-24 09:20:41 +08001142 }
1143 if (!value) {
Markus Armbrusterd1c81c32022-11-21 09:50:53 +01001144 error_setg(errp, "Property '%s.%s' requires a positive value",
Zhang Chena2e5cb72020-06-24 09:20:41 +08001145 object_get_typename(obj), name);
Markus Armbrusterd1c81c32022-11-21 09:50:53 +01001146 return;
Zhang Chena2e5cb72020-06-24 09:20:41 +08001147 }
1148 max_queue_size = value;
Zhang Chena2e5cb72020-06-24 09:20:41 +08001149}
1150
Zhang Chen7dce4e62016-09-27 10:22:26 +08001151static void compare_pri_rs_finalize(SocketReadState *pri_rs)
1152{
Zhang Chen59509ec2016-09-27 10:22:27 +08001153 CompareState *s = container_of(pri_rs, CompareState, pri_rs);
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001154 Connection *conn = NULL;
Zhang Chen59509ec2016-09-27 10:22:27 +08001155
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001156 if (packet_enqueue(s, PRIMARY_IN, &conn)) {
Zhang Chen59509ec2016-09-27 10:22:27 +08001157 trace_colo_compare_main("primary: unsupported packet in");
Zhang Chenaa3a7032017-07-04 14:53:52 +08001158 compare_chr_send(s,
1159 pri_rs->buf,
1160 pri_rs->packet_len,
Zhang Chen30685c02019-06-10 00:44:31 +08001161 pri_rs->vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +08001162 false,
Zhang Chen30685c02019-06-10 00:44:31 +08001163 false);
Zhang Chen0682e152016-09-27 10:22:30 +08001164 } else {
Mao Zhongyi34632182017-10-13 14:32:08 +08001165 /* compare packet in the specified connection */
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001166 colo_compare_connection(conn, s);
Zhang Chen59509ec2016-09-27 10:22:27 +08001167 }
Zhang Chen7dce4e62016-09-27 10:22:26 +08001168}
1169
1170static void compare_sec_rs_finalize(SocketReadState *sec_rs)
1171{
Zhang Chen59509ec2016-09-27 10:22:27 +08001172 CompareState *s = container_of(sec_rs, CompareState, sec_rs);
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001173 Connection *conn = NULL;
Zhang Chen59509ec2016-09-27 10:22:27 +08001174
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001175 if (packet_enqueue(s, SECONDARY_IN, &conn)) {
Zhang Chen59509ec2016-09-27 10:22:27 +08001176 trace_colo_compare_main("secondary: unsupported packet in");
Zhang Chen0682e152016-09-27 10:22:30 +08001177 } else {
Mao Zhongyi34632182017-10-13 14:32:08 +08001178 /* compare packet in the specified connection */
Mao Zhongyi8ec14402017-10-13 14:32:07 +08001179 colo_compare_connection(conn, s);
Zhang Chen59509ec2016-09-27 10:22:27 +08001180 }
Zhang Chen7dce4e62016-09-27 10:22:26 +08001181}
1182
Zhang Chen13025fe2019-06-10 00:44:30 +08001183static void compare_notify_rs_finalize(SocketReadState *notify_rs)
1184{
Zhang Chen1d09f702019-06-10 00:44:32 +08001185 CompareState *s = container_of(notify_rs, CompareState, notify_rs);
1186
Zhang Chenf77bed12019-07-23 01:18:28 +08001187 const char msg[] = "COLO_COMPARE_GET_XEN_INIT";
Zhang Chen1d09f702019-06-10 00:44:32 +08001188 int ret;
1189
Zhang Chenf77bed12019-07-23 01:18:28 +08001190 if (packet_matches_str("COLO_USERSPACE_PROXY_INIT",
1191 notify_rs->buf,
1192 notify_rs->packet_len)) {
Lukas Straub9c55fe92020-05-22 15:53:53 +08001193 ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false);
Zhang Chen1d09f702019-06-10 00:44:32 +08001194 if (ret < 0) {
1195 error_report("Notify Xen COLO-frame INIT failed");
1196 }
Zhang Chenf77bed12019-07-23 01:18:28 +08001197 } else if (packet_matches_str("COLO_CHECKPOINT",
1198 notify_rs->buf,
1199 notify_rs->packet_len)) {
Zhang Chen1d09f702019-06-10 00:44:32 +08001200 /* colo-compare do checkpoint, flush pri packet and remove sec packet */
1201 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
Zhang Chenf77bed12019-07-23 01:18:28 +08001202 } else {
1203 error_report("COLO compare got unsupported instruction");
Zhang Chen1d09f702019-06-10 00:44:32 +08001204 }
Zhang Chen13025fe2019-06-10 00:44:30 +08001205}
Zhang Chen7dce4e62016-09-27 10:22:26 +08001206
1207/*
1208 * Return 0 is success.
1209 * Return 1 is failed.
1210 */
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001211static int find_and_check_chardev(Chardev **chr,
Zhang Chen7dce4e62016-09-27 10:22:26 +08001212 char *chr_name,
1213 Error **errp)
1214{
Zhang Chen7dce4e62016-09-27 10:22:26 +08001215 *chr = qemu_chr_find(chr_name);
1216 if (*chr == NULL) {
1217 error_setg(errp, "Device '%s' not found",
1218 chr_name);
1219 return 1;
1220 }
1221
Daniel P. Berrange0a733362016-10-07 13:18:34 +01001222 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
1223 error_setg(errp, "chardev \"%s\" is not reconnectable",
Zhang Chen7dce4e62016-09-27 10:22:26 +08001224 chr_name);
1225 return 1;
1226 }
Marc-André Lureaufbf3cc32016-10-22 12:52:54 +03001227
Marc-André Lureau269d25c2018-12-06 00:37:35 +04001228 if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_GCONTEXT)) {
1229 error_setg(errp, "chardev \"%s\" cannot switch context",
1230 chr_name);
1231 return 1;
1232 }
1233
Zhang Chen7dce4e62016-09-27 10:22:26 +08001234 return 0;
1235}
1236
1237/*
1238 * Called from the main thread on the primary
1239 * to setup colo-compare.
1240 */
1241static void colo_compare_complete(UserCreatable *uc, Error **errp)
1242{
1243 CompareState *s = COLO_COMPARE(uc);
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001244 Chardev *chr;
Zhang Chen7dce4e62016-09-27 10:22:26 +08001245
Wang Yongdd321ec2017-08-29 15:22:38 +08001246 if (!s->pri_indev || !s->sec_indev || !s->outdev || !s->iothread) {
Zhang Chen7dce4e62016-09-27 10:22:26 +08001247 error_setg(errp, "colo compare needs 'primary_in' ,"
Wang Yongdd321ec2017-08-29 15:22:38 +08001248 "'secondary_in','outdev','iothread' property set");
Zhang Chen7dce4e62016-09-27 10:22:26 +08001249 return;
1250 } else if (!strcmp(s->pri_indev, s->outdev) ||
1251 !strcmp(s->sec_indev, s->outdev) ||
1252 !strcmp(s->pri_indev, s->sec_indev)) {
1253 error_setg(errp, "'indev' and 'outdev' could not be same "
1254 "for compare module");
1255 return;
1256 }
1257
Zhang Chen9cc43c92020-03-18 16:23:19 +08001258 if (!s->compare_timeout) {
1259 /* Set default value to 3000 MS */
1260 s->compare_timeout = DEFAULT_TIME_OUT_MS;
1261 }
1262
Zhang Chencca35ac2020-03-18 16:23:20 +08001263 if (!s->expired_scan_cycle) {
Zhang Chen09313cd2022-01-14 13:09:01 +08001264 /* Set default value to 1000 MS */
Zhang Chencca35ac2020-03-18 16:23:20 +08001265 s->expired_scan_cycle = REGULAR_PACKET_CHECK_MS;
1266 }
1267
Zhang Chena2e5cb72020-06-24 09:20:41 +08001268 if (!max_queue_size) {
1269 /* Set default queue size to 1024 */
1270 max_queue_size = MAX_QUEUE_SIZE;
1271 }
1272
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001273 if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
1274 !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
Zhang Chen7dce4e62016-09-27 10:22:26 +08001275 return;
1276 }
1277
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001278 if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
1279 !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
Zhang Chen7dce4e62016-09-27 10:22:26 +08001280 return;
1281 }
1282
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001283 if (find_and_check_chardev(&chr, s->outdev, errp) ||
1284 !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
Zhang Chen7dce4e62016-09-27 10:22:26 +08001285 return;
1286 }
1287
Zhang Chenaa3a7032017-07-04 14:53:52 +08001288 net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
1289 net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001290
Zhang Chen13025fe2019-06-10 00:44:30 +08001291 /* Try to enable remote notify chardev, currently just for Xen COLO */
1292 if (s->notify_dev) {
1293 if (find_and_check_chardev(&chr, s->notify_dev, errp) ||
1294 !qemu_chr_fe_init(&s->chr_notify_dev, chr, errp)) {
1295 return;
1296 }
1297
1298 net_socket_rs_init(&s->notify_rs, compare_notify_rs_finalize,
1299 s->vnet_hdr);
1300 }
1301
Lukas Straub9c55fe92020-05-22 15:53:53 +08001302 s->out_sendco.s = s;
1303 s->out_sendco.chr = &s->chr_out;
1304 s->out_sendco.notify_remote_frame = false;
1305 s->out_sendco.done = true;
1306 g_queue_init(&s->out_sendco.send_list);
1307
1308 if (s->notify_dev) {
1309 s->notify_sendco.s = s;
1310 s->notify_sendco.chr = &s->chr_notify_dev;
1311 s->notify_sendco.notify_remote_frame = true;
1312 s->notify_sendco.done = true;
1313 g_queue_init(&s->notify_sendco.send_list);
1314 }
1315
Zhang Chenb6540d42016-09-27 10:22:29 +08001316 g_queue_init(&s->conn_list);
1317
1318 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
1319 connection_key_equal,
1320 g_free,
Zhang Chena18d4362022-04-01 11:47:00 +08001321 NULL);
Zhang Chen59509ec2016-09-27 10:22:27 +08001322
Wang Yongdd321ec2017-08-29 15:22:38 +08001323 colo_compare_iothread(s);
Lukas Straub5bd57eb2020-05-22 15:53:56 +08001324
1325 qemu_mutex_lock(&colo_compare_mutex);
1326 if (!colo_compare_active) {
1327 qemu_mutex_init(&event_mtx);
1328 qemu_cond_init(&event_complete_cond);
1329 colo_compare_active = true;
1330 }
1331 QTAILQ_INSERT_TAIL(&net_compares, s, next);
1332 qemu_mutex_unlock(&colo_compare_mutex);
1333
Zhang Chen7dce4e62016-09-27 10:22:26 +08001334 return;
1335}
1336
zhanghailiangdfd917a2017-02-17 10:53:12 +08001337static void colo_flush_packets(void *opaque, void *user_data)
1338{
1339 CompareState *s = user_data;
1340 Connection *conn = opaque;
1341 Packet *pkt = NULL;
1342
1343 while (!g_queue_is_empty(&conn->primary_list)) {
Zhang Chena5f038e2022-01-14 13:09:00 +08001344 pkt = g_queue_pop_tail(&conn->primary_list);
Zhang Chenaa3a7032017-07-04 14:53:52 +08001345 compare_chr_send(s,
1346 pkt->data,
1347 pkt->size,
Zhang Chen30685c02019-06-10 00:44:31 +08001348 pkt->vnet_hdr_len,
Lukas Straub9c55fe92020-05-22 15:53:53 +08001349 false,
1350 true);
1351 packet_destroy_partial(pkt, NULL);
zhanghailiangdfd917a2017-02-17 10:53:12 +08001352 }
1353 while (!g_queue_is_empty(&conn->secondary_list)) {
Zhang Chena5f038e2022-01-14 13:09:00 +08001354 pkt = g_queue_pop_tail(&conn->secondary_list);
zhanghailiangdfd917a2017-02-17 10:53:12 +08001355 packet_destroy(pkt, NULL);
1356 }
1357}
1358
Zhang Chen7dce4e62016-09-27 10:22:26 +08001359static void colo_compare_class_init(ObjectClass *oc, void *data)
1360{
1361 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
1362
1363 ucc->complete = colo_compare_complete;
1364}
1365
1366static void colo_compare_init(Object *obj)
1367{
Zhang Chenaa3a7032017-07-04 14:53:52 +08001368 CompareState *s = COLO_COMPARE(obj);
1369
Zhang Chen7dce4e62016-09-27 10:22:26 +08001370 object_property_add_str(obj, "primary_in",
Markus Armbrusterd2623122020-05-05 17:29:22 +02001371 compare_get_pri_indev, compare_set_pri_indev);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001372 object_property_add_str(obj, "secondary_in",
Markus Armbrusterd2623122020-05-05 17:29:22 +02001373 compare_get_sec_indev, compare_set_sec_indev);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001374 object_property_add_str(obj, "outdev",
Markus Armbrusterd2623122020-05-05 17:29:22 +02001375 compare_get_outdev, compare_set_outdev);
Wang Yongdd321ec2017-08-29 15:22:38 +08001376 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
1377 (Object **)&s->iothread,
1378 object_property_allow_set_link,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001379 OBJ_PROP_LINK_STRONG);
Zhang Chencf6af762019-06-10 00:44:29 +08001380 /* This parameter just for Xen COLO */
1381 object_property_add_str(obj, "notify_dev",
Markus Armbrusterd2623122020-05-05 17:29:22 +02001382 compare_get_notify_dev, compare_set_notify_dev);
Zhang Chenaa3a7032017-07-04 14:53:52 +08001383
Zhang Chen0c4266e2020-10-16 13:52:05 +08001384 object_property_add(obj, "compare_timeout", "uint64",
Zhang Chen9cc43c92020-03-18 16:23:19 +08001385 compare_get_timeout,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001386 compare_set_timeout, NULL, NULL);
Zhang Chen9cc43c92020-03-18 16:23:19 +08001387
Zhang Chencca35ac2020-03-18 16:23:20 +08001388 object_property_add(obj, "expired_scan_cycle", "uint32",
1389 compare_get_expired_scan_cycle,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001390 compare_set_expired_scan_cycle, NULL, NULL);
Zhang Chencca35ac2020-03-18 16:23:20 +08001391
Zhang Chena2e5cb72020-06-24 09:20:41 +08001392 object_property_add(obj, "max_queue_size", "uint32",
1393 get_max_queue_size,
1394 set_max_queue_size, NULL, NULL);
1395
Zhang Chenaa3a7032017-07-04 14:53:52 +08001396 s->vnet_hdr = false;
1397 object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001398 compare_set_vnet_hdr);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001399}
1400
Rao, Lei0c7af1a2021-06-08 16:23:30 +08001401void colo_compare_cleanup(void)
1402{
1403 CompareState *tmp = NULL;
1404 CompareState *n = NULL;
1405
1406 QTAILQ_FOREACH_SAFE(tmp, &net_compares, next, n) {
1407 object_unparent(OBJECT(tmp));
1408 }
1409}
1410
Zhang Chen7dce4e62016-09-27 10:22:26 +08001411static void colo_compare_finalize(Object *obj)
1412{
1413 CompareState *s = COLO_COMPARE(obj);
Zhang Chen0ffcece2018-09-03 12:38:43 +08001414 CompareState *tmp = NULL;
Zhang Chen7dce4e62016-09-27 10:22:26 +08001415
Lukas Straub45942b72020-05-22 15:53:55 +08001416 qemu_mutex_lock(&colo_compare_mutex);
Zhang Chen0ffcece2018-09-03 12:38:43 +08001417 QTAILQ_FOREACH(tmp, &net_compares, next) {
1418 if (tmp == s) {
1419 QTAILQ_REMOVE(&net_compares, s, next);
1420 break;
1421 }
1422 }
Lukas Straub45942b72020-05-22 15:53:55 +08001423 if (QTAILQ_EMPTY(&net_compares)) {
1424 colo_compare_active = false;
1425 qemu_mutex_destroy(&event_mtx);
1426 qemu_cond_destroy(&event_complete_cond);
1427 }
1428 qemu_mutex_unlock(&colo_compare_mutex);
Zhang Chen0ffcece2018-09-03 12:38:43 +08001429
Lukas Straub5bd57eb2020-05-22 15:53:56 +08001430 qemu_chr_fe_deinit(&s->chr_pri_in, false);
1431 qemu_chr_fe_deinit(&s->chr_sec_in, false);
1432 qemu_chr_fe_deinit(&s->chr_out, false);
1433 if (s->notify_dev) {
1434 qemu_chr_fe_deinit(&s->chr_notify_dev, false);
1435 }
1436
Lukas Straubf81cddf2020-07-31 07:06:04 +02001437 colo_compare_timer_del(s);
Lukas Straub5bd57eb2020-05-22 15:53:56 +08001438
1439 qemu_bh_delete(s->event_bh);
1440
Lukas Straub9c55fe92020-05-22 15:53:53 +08001441 AioContext *ctx = iothread_get_aio_context(s->iothread);
Lukas Straub9c55fe92020-05-22 15:53:53 +08001442 AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
1443 if (s->notify_dev) {
1444 AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
1445 }
Lukas Straub9c55fe92020-05-22 15:53:53 +08001446
zhanghailiangdfd917a2017-02-17 10:53:12 +08001447 /* Release all unhandled packets after compare thead exited */
1448 g_queue_foreach(&s->conn_list, colo_flush_packets, s);
Lukas Straub9c55fe92020-05-22 15:53:53 +08001449 AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
zhanghailiangdfd917a2017-02-17 10:53:12 +08001450
Zhang Chen727c2d72017-02-22 13:16:06 +08001451 g_queue_clear(&s->conn_list);
Lukas Straub9c55fe92020-05-22 15:53:53 +08001452 g_queue_clear(&s->out_sendco.send_list);
1453 if (s->notify_dev) {
1454 g_queue_clear(&s->notify_sendco.send_list);
1455 }
Zhang Chenb6540d42016-09-27 10:22:29 +08001456
Wang Yongdd321ec2017-08-29 15:22:38 +08001457 if (s->connection_track_table) {
1458 g_hash_table_destroy(s->connection_track_table);
1459 }
1460
Lukas Straubf81cddf2020-07-31 07:06:04 +02001461 object_unref(OBJECT(s->iothread));
Zhang Chen0ffcece2018-09-03 12:38:43 +08001462
Zhang Chen7dce4e62016-09-27 10:22:26 +08001463 g_free(s->pri_indev);
1464 g_free(s->sec_indev);
1465 g_free(s->outdev);
Zhang Chencf6af762019-06-10 00:44:29 +08001466 g_free(s->notify_dev);
Zhang Chen7dce4e62016-09-27 10:22:26 +08001467}
1468
Lukas Straub45942b72020-05-22 15:53:55 +08001469static void __attribute__((__constructor__)) colo_compare_init_globals(void)
1470{
1471 colo_compare_active = false;
1472 qemu_mutex_init(&colo_compare_mutex);
1473}
1474
Zhang Chen7dce4e62016-09-27 10:22:26 +08001475static const TypeInfo colo_compare_info = {
1476 .name = TYPE_COLO_COMPARE,
1477 .parent = TYPE_OBJECT,
1478 .instance_size = sizeof(CompareState),
1479 .instance_init = colo_compare_init,
1480 .instance_finalize = colo_compare_finalize,
1481 .class_size = sizeof(CompareClass),
1482 .class_init = colo_compare_class_init,
1483 .interfaces = (InterfaceInfo[]) {
1484 { TYPE_USER_CREATABLE },
1485 { }
1486 }
1487};
1488
1489static void register_types(void)
1490{
1491 type_register_static(&colo_compare_info);
1492}
1493
1494type_init(register_types);