]>
Commit | Line | Data |
---|---|---|
7dce4e6f ZC |
1 | /* |
2 | * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO) | |
3 | * (a.k.a. Fault Tolerance or Continuous Replication) | |
4 | * | |
5 | * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. | |
6 | * Copyright (c) 2016 FUJITSU LIMITED | |
7 | * Copyright (c) 2016 Intel Corporation | |
8 | * | |
9 | * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
12 | * later. See the COPYING file in the top-level directory. | |
13 | */ | |
14 | ||
15 | #include "qemu/osdep.h" | |
a8d25326 | 16 | #include "qemu-common.h" |
7dce4e6f | 17 | #include "qemu/error-report.h" |
59509ec1 | 18 | #include "trace.h" |
7dce4e6f ZC |
19 | #include "qapi/error.h" |
20 | #include "net/net.h" | |
f4b61836 | 21 | #include "net/eth.h" |
7dce4e6f ZC |
22 | #include "qom/object_interfaces.h" |
23 | #include "qemu/iov.h" | |
24 | #include "qom/object.h" | |
7dce4e6f | 25 | #include "net/queue.h" |
4d43a603 | 26 | #include "chardev/char-fe.h" |
7dce4e6f | 27 | #include "qemu/sockets.h" |
f27f01db | 28 | #include "colo.h" |
dd321ecf | 29 | #include "sysemu/iothread.h" |
0ffcece3 ZC |
30 | #include "net/colo-compare.h" |
31 | #include "migration/colo.h" | |
dccd0313 | 32 | #include "migration/migration.h" |
e05ae1d9 | 33 | #include "util.h" |
7dce4e6f | 34 | |
9c55fe94 LS |
35 | #include "block/aio-wait.h" |
36 | #include "qemu/coroutine.h" | |
37 | ||
7dce4e6f | 38 | #define TYPE_COLO_COMPARE "colo-compare" |
db1015e9 | 39 | typedef struct CompareState CompareState; |
7dce4e6f ZC |
40 | #define COLO_COMPARE(obj) \ |
41 | OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE) | |
42 | ||
0ffcece3 ZC |
43 | static QTAILQ_HEAD(, CompareState) net_compares = |
44 | QTAILQ_HEAD_INITIALIZER(net_compares); | |
45 | ||
dccd0313 ZC |
46 | static NotifierList colo_compare_notifiers = |
47 | NOTIFIER_LIST_INITIALIZER(colo_compare_notifiers); | |
48 | ||
0682e15b | 49 | #define COMPARE_READ_LEN_MAX NET_BUFSIZE |
b6540d40 ZC |
50 | #define MAX_QUEUE_SIZE 1024 |
51 | ||
f449c9e5 MZ |
52 | #define COLO_COMPARE_FREE_PRIMARY 0x01 |
53 | #define COLO_COMPARE_FREE_SECONDARY 0x02 | |
54 | ||
0682e15b | 55 | #define REGULAR_PACKET_CHECK_MS 3000 |
9cc43c94 | 56 | #define DEFAULT_TIME_OUT_MS 3000 |
0682e15b | 57 | |
45942b79 LS |
58 | static QemuMutex colo_compare_mutex; |
59 | static bool colo_compare_active; | |
0ffcece3 ZC |
60 | static QemuMutex event_mtx; |
61 | static QemuCond event_complete_cond; | |
62 | static int event_unhandled_count; | |
a2e5cb7a | 63 | static uint32_t max_queue_size; |
0ffcece3 | 64 | |
59509ec1 | 65 | /* |
61c5f469 ZC |
66 | * + CompareState ++ |
67 | * | | | |
68 | * +---------------+ +---------------+ +---------------+ | |
69 | * | conn list + - > conn + ------- > conn + -- > ...... | |
70 | * +---------------+ +---------------+ +---------------+ | |
71 | * | | | | | | | |
72 | * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+ | |
73 | * |primary | |secondary |primary | |secondary | |
74 | * |packet | |packet + |packet | |packet + | |
75 | * +--------+ +--------+ +--------+ +--------+ | |
76 | * | | | | | |
77 | * +---v----+ +---v----+ +---v----+ +---v----+ | |
78 | * |primary | |secondary |primary | |secondary | |
79 | * |packet | |packet + |packet | |packet + | |
80 | * +--------+ +--------+ +--------+ +--------+ | |
81 | * | | | | | |
82 | * +---v----+ +---v----+ +---v----+ +---v----+ | |
83 | * |primary | |secondary |primary | |secondary | |
84 | * |packet | |packet + |packet | |packet + | |
85 | * +--------+ +--------+ +--------+ +--------+ | |
86 | */ | |
9c55fe94 LS |
87 | |
88 | typedef struct SendCo { | |
89 | Coroutine *co; | |
90 | struct CompareState *s; | |
91 | CharBackend *chr; | |
92 | GQueue send_list; | |
93 | bool notify_remote_frame; | |
94 | bool done; | |
95 | int ret; | |
96 | } SendCo; | |
97 | ||
98 | typedef struct SendEntry { | |
99 | uint32_t size; | |
100 | uint32_t vnet_hdr_len; | |
101 | uint8_t *buf; | |
102 | } SendEntry; | |
103 | ||
db1015e9 | 104 | struct CompareState { |
7dce4e6f ZC |
105 | Object parent; |
106 | ||
107 | char *pri_indev; | |
108 | char *sec_indev; | |
109 | char *outdev; | |
cf6af766 | 110 | char *notify_dev; |
32a6ebec MAL |
111 | CharBackend chr_pri_in; |
112 | CharBackend chr_sec_in; | |
113 | CharBackend chr_out; | |
13025fee | 114 | CharBackend chr_notify_dev; |
7dce4e6f ZC |
115 | SocketReadState pri_rs; |
116 | SocketReadState sec_rs; | |
13025fee | 117 | SocketReadState notify_rs; |
9c55fe94 LS |
118 | SendCo out_sendco; |
119 | SendCo notify_sendco; | |
aa3a7032 | 120 | bool vnet_hdr; |
9cc43c94 | 121 | uint32_t compare_timeout; |
cca35ac4 | 122 | uint32_t expired_scan_cycle; |
59509ec1 | 123 | |
61c5f469 ZC |
124 | /* |
125 | * Record the connection that through the NIC | |
126 | * Element type: Connection | |
b6540d40 ZC |
127 | */ |
128 | GQueue conn_list; | |
61c5f469 | 129 | /* Record the connection without repetition */ |
59509ec1 | 130 | GHashTable *connection_track_table; |
dfd917a9 | 131 | |
dd321ecf | 132 | IOThread *iothread; |
b43decb0 | 133 | GMainContext *worker_context; |
dd321ecf | 134 | QEMUTimer *packet_check_timer; |
0ffcece3 ZC |
135 | |
136 | QEMUBH *event_bh; | |
137 | enum colo_event event; | |
138 | ||
139 | QTAILQ_ENTRY(CompareState) next; | |
db1015e9 | 140 | }; |
7dce4e6f ZC |
141 | |
142 | typedef struct CompareClass { | |
143 | ObjectClass parent_class; | |
144 | } CompareClass; | |
145 | ||
59509ec1 ZC |
146 | enum { |
147 | PRIMARY_IN = 0, | |
148 | SECONDARY_IN, | |
149 | }; | |
150 | ||
bdadbb0f DS |
151 | static const char *colo_mode[] = { |
152 | [PRIMARY_IN] = "primary", | |
153 | [SECONDARY_IN] = "secondary", | |
154 | }; | |
24525e93 | 155 | |
3037e7a5 | 156 | static int compare_chr_send(CompareState *s, |
9c55fe94 | 157 | uint8_t *buf, |
aa3a7032 | 158 | uint32_t size, |
30685c00 | 159 | uint32_t vnet_hdr_len, |
9c55fe94 LS |
160 | bool notify_remote_frame, |
161 | bool zero_copy); | |
59509ec1 | 162 | |
f77bed14 ZC |
163 | static bool packet_matches_str(const char *str, |
164 | const uint8_t *buf, | |
165 | uint32_t packet_len) | |
166 | { | |
167 | if (packet_len != strlen(str)) { | |
168 | return false; | |
169 | } | |
170 | ||
171 | return !memcmp(str, buf, strlen(str)); | |
172 | } | |
173 | ||
1d09f700 ZC |
174 | static void notify_remote_frame(CompareState *s) |
175 | { | |
176 | char msg[] = "DO_CHECKPOINT"; | |
177 | int ret = 0; | |
178 | ||
9c55fe94 | 179 | ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false); |
1d09f700 ZC |
180 | if (ret < 0) { |
181 | error_report("Notify Xen COLO-frame failed"); | |
182 | } | |
183 | } | |
184 | ||
185 | static void colo_compare_inconsistency_notify(CompareState *s) | |
186 | { | |
187 | if (s->notify_dev) { | |
188 | notify_remote_frame(s); | |
189 | } else { | |
190 | notifier_list_notify(&colo_compare_notifiers, | |
191 | migrate_get_current()); | |
192 | } | |
193 | } | |
194 | ||
a935cc31 ZC |
195 | static gint seq_sorter(Packet *a, Packet *b, gpointer data) |
196 | { | |
e05ae1d9 | 197 | struct tcp_hdr *atcp, *btcp; |
a935cc31 | 198 | |
e05ae1d9 MAL |
199 | atcp = (struct tcp_hdr *)(a->transport_header); |
200 | btcp = (struct tcp_hdr *)(b->transport_header); | |
a935cc31 ZC |
201 | return ntohl(atcp->th_seq) - ntohl(btcp->th_seq); |
202 | } | |
203 | ||
f449c9e5 MZ |
204 | static void fill_pkt_tcp_info(void *data, uint32_t *max_ack) |
205 | { | |
206 | Packet *pkt = data; | |
e05ae1d9 | 207 | struct tcp_hdr *tcphd; |
f449c9e5 | 208 | |
e05ae1d9 | 209 | tcphd = (struct tcp_hdr *)pkt->transport_header; |
f449c9e5 MZ |
210 | |
211 | pkt->tcp_seq = ntohl(tcphd->th_seq); | |
212 | pkt->tcp_ack = ntohl(tcphd->th_ack); | |
213 | *max_ack = *max_ack > pkt->tcp_ack ? *max_ack : pkt->tcp_ack; | |
214 | pkt->header_size = pkt->transport_header - (uint8_t *)pkt->data | |
215 | + (tcphd->th_off << 2) - pkt->vnet_hdr_len; | |
216 | pkt->payload_size = pkt->size - pkt->header_size; | |
217 | pkt->seq_end = pkt->tcp_seq + pkt->payload_size; | |
218 | pkt->flags = tcphd->th_flags; | |
219 | } | |
220 | ||
8850d4ca MZ |
221 | /* |
222 | * Return 1 on success, if return 0 means the | |
223 | * packet will be dropped | |
224 | */ | |
f449c9e5 | 225 | static int colo_insert_packet(GQueue *queue, Packet *pkt, uint32_t *max_ack) |
8850d4ca | 226 | { |
a2e5cb7a | 227 | if (g_queue_get_length(queue) <= max_queue_size) { |
8850d4ca | 228 | if (pkt->ip->ip_p == IPPROTO_TCP) { |
f449c9e5 | 229 | fill_pkt_tcp_info(pkt, max_ack); |
8850d4ca MZ |
230 | g_queue_insert_sorted(queue, |
231 | pkt, | |
232 | (GCompareDataFunc)seq_sorter, | |
233 | NULL); | |
234 | } else { | |
235 | g_queue_push_tail(queue, pkt); | |
236 | } | |
237 | return 1; | |
238 | } | |
239 | return 0; | |
240 | } | |
241 | ||
59509ec1 ZC |
242 | /* |
243 | * Return 0 on success, if return -1 means the pkt | |
244 | * is unsupported(arp and ipv6) and will be sent later | |
245 | */ | |
8ec14402 | 246 | static int packet_enqueue(CompareState *s, int mode, Connection **con) |
59509ec1 | 247 | { |
b6540d40 | 248 | ConnectionKey key; |
59509ec1 | 249 | Packet *pkt = NULL; |
b6540d40 | 250 | Connection *conn; |
bdadbb0f | 251 | int ret; |
59509ec1 ZC |
252 | |
253 | if (mode == PRIMARY_IN) { | |
ada1a33f ZC |
254 | pkt = packet_new(s->pri_rs.buf, |
255 | s->pri_rs.packet_len, | |
256 | s->pri_rs.vnet_hdr_len); | |
59509ec1 | 257 | } else { |
ada1a33f ZC |
258 | pkt = packet_new(s->sec_rs.buf, |
259 | s->sec_rs.packet_len, | |
260 | s->sec_rs.vnet_hdr_len); | |
59509ec1 ZC |
261 | } |
262 | ||
263 | if (parse_packet_early(pkt)) { | |
264 | packet_destroy(pkt, NULL); | |
265 | pkt = NULL; | |
266 | return -1; | |
267 | } | |
b6540d40 | 268 | fill_connection_key(pkt, &key); |
59509ec1 | 269 | |
b6540d40 ZC |
270 | conn = connection_get(s->connection_track_table, |
271 | &key, | |
272 | &s->conn_list); | |
59509ec1 | 273 | |
b6540d40 ZC |
274 | if (!conn->processing) { |
275 | g_queue_push_tail(&s->conn_list, conn); | |
276 | conn->processing = true; | |
277 | } | |
278 | ||
279 | if (mode == PRIMARY_IN) { | |
bdadbb0f | 280 | ret = colo_insert_packet(&conn->primary_list, pkt, &conn->pack); |
b6540d40 | 281 | } else { |
bdadbb0f | 282 | ret = colo_insert_packet(&conn->secondary_list, pkt, &conn->sack); |
b6540d40 | 283 | } |
bdadbb0f DS |
284 | |
285 | if (!ret) { | |
286 | trace_colo_compare_drop_packet(colo_mode[mode], | |
287 | "queue size too big, drop packet"); | |
288 | packet_destroy(pkt, NULL); | |
289 | pkt = NULL; | |
290 | } | |
291 | ||
4d366235 | 292 | *con = conn; |
59509ec1 ZC |
293 | |
294 | return 0; | |
295 | } | |
296 | ||
f449c9e5 MZ |
297 | static inline bool after(uint32_t seq1, uint32_t seq2) |
298 | { | |
299 | return (int32_t)(seq1 - seq2) > 0; | |
300 | } | |
301 | ||
302 | static void colo_release_primary_pkt(CompareState *s, Packet *pkt) | |
303 | { | |
304 | int ret; | |
305 | ret = compare_chr_send(s, | |
306 | pkt->data, | |
307 | pkt->size, | |
30685c00 | 308 | pkt->vnet_hdr_len, |
9c55fe94 LS |
309 | false, |
310 | true); | |
f449c9e5 MZ |
311 | if (ret < 0) { |
312 | error_report("colo send primary packet failed"); | |
313 | } | |
314 | trace_colo_compare_main("packet same and release packet"); | |
9c55fe94 | 315 | packet_destroy_partial(pkt, NULL); |
f449c9e5 MZ |
316 | } |
317 | ||
0682e15b ZC |
318 | /* |
319 | * The IP packets sent by primary and secondary | |
320 | * will be compared in here | |
321 | * TODO support ip fragment, Out-Of-Order | |
322 | * return: 0 means packet same | |
323 | * > 0 || < 0 means packet different | |
324 | */ | |
9394133f MZ |
325 | static int colo_compare_packet_payload(Packet *ppkt, |
326 | Packet *spkt, | |
327 | uint16_t poffset, | |
328 | uint16_t soffset, | |
329 | uint16_t len) | |
330 | ||
0682e15b | 331 | { |
d87aa138 | 332 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
e630b2bf ZC |
333 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; |
334 | ||
335 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
336 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
337 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
338 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
339 | ||
340 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
341 | pri_ip_dst, spkt->size, | |
342 | sec_ip_src, sec_ip_dst); | |
343 | } | |
0682e15b | 344 | |
9394133f | 345 | return memcmp(ppkt->data + poffset, spkt->data + soffset, len); |
0682e15b ZC |
346 | } |
347 | ||
f4b61836 | 348 | /* |
f449c9e5 MZ |
349 | * return true means that the payload is consist and |
350 | * need to make the next comparison, false means do | |
351 | * the checkpoint | |
352 | */ | |
353 | static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt, | |
354 | int8_t *mark, uint32_t max_ack) | |
0682e15b | 355 | { |
f449c9e5 MZ |
356 | *mark = 0; |
357 | ||
f449c9e5 | 358 | if (ppkt->tcp_seq == spkt->tcp_seq && ppkt->seq_end == spkt->seq_end) { |
1e907a32 | 359 | if (!colo_compare_packet_payload(ppkt, spkt, |
f449c9e5 MZ |
360 | ppkt->header_size, spkt->header_size, |
361 | ppkt->payload_size)) { | |
362 | *mark = COLO_COMPARE_FREE_SECONDARY | COLO_COMPARE_FREE_PRIMARY; | |
363 | return true; | |
364 | } | |
365 | } | |
f4b61836 | 366 | |
f449c9e5 MZ |
367 | /* one part of secondary packet payload still need to be compared */ |
368 | if (!after(ppkt->seq_end, spkt->seq_end)) { | |
1e907a32 | 369 | if (!colo_compare_packet_payload(ppkt, spkt, |
f449c9e5 MZ |
370 | ppkt->header_size + ppkt->offset, |
371 | spkt->header_size + spkt->offset, | |
372 | ppkt->payload_size - ppkt->offset)) { | |
373 | if (!after(ppkt->tcp_ack, max_ack)) { | |
374 | *mark = COLO_COMPARE_FREE_PRIMARY; | |
375 | spkt->offset += ppkt->payload_size - ppkt->offset; | |
376 | return true; | |
377 | } else { | |
378 | /* secondary guest hasn't ack the data, don't send | |
379 | * out this packet | |
380 | */ | |
381 | return false; | |
382 | } | |
383 | } | |
384 | } else { | |
385 | /* primary packet is longer than secondary packet, compare | |
386 | * the same part and mark the primary packet offset | |
387 | */ | |
1e907a32 | 388 | if (!colo_compare_packet_payload(ppkt, spkt, |
f449c9e5 MZ |
389 | ppkt->header_size + ppkt->offset, |
390 | spkt->header_size + spkt->offset, | |
391 | spkt->payload_size - spkt->offset)) { | |
392 | *mark = COLO_COMPARE_FREE_SECONDARY; | |
393 | ppkt->offset += spkt->payload_size - spkt->offset; | |
394 | return true; | |
395 | } | |
396 | } | |
397 | ||
398 | return false; | |
399 | } | |
2ad7ca4c | 400 | |
f449c9e5 MZ |
401 | static void colo_compare_tcp(CompareState *s, Connection *conn) |
402 | { | |
403 | Packet *ppkt = NULL, *spkt = NULL; | |
404 | int8_t mark; | |
f4b61836 ZC |
405 | |
406 | /* | |
f449c9e5 MZ |
407 | * If ppkt and spkt have the same payload, but ppkt's ACK |
408 | * is greater than spkt's ACK, in this case we can not | |
409 | * send the ppkt because it will cause the secondary guest | |
410 | * to miss sending some data in the next. Therefore, we | |
411 | * record the maximum ACK in the current queue at both | |
412 | * primary side and secondary side. Only when the ack is | |
413 | * less than the smaller of the two maximum ack, then we | |
414 | * can ensure that the packet's payload is acknowledged by | |
415 | * primary and secondary. | |
416 | */ | |
417 | uint32_t min_ack = conn->pack > conn->sack ? conn->sack : conn->pack; | |
418 | ||
419 | pri: | |
420 | if (g_queue_is_empty(&conn->primary_list)) { | |
421 | return; | |
422 | } | |
423 | ppkt = g_queue_pop_head(&conn->primary_list); | |
424 | sec: | |
425 | if (g_queue_is_empty(&conn->secondary_list)) { | |
426 | g_queue_push_head(&conn->primary_list, ppkt); | |
427 | return; | |
f4b61836 | 428 | } |
f449c9e5 | 429 | spkt = g_queue_pop_head(&conn->secondary_list); |
f4b61836 | 430 | |
f449c9e5 MZ |
431 | if (ppkt->tcp_seq == ppkt->seq_end) { |
432 | colo_release_primary_pkt(s, ppkt); | |
433 | ppkt = NULL; | |
434 | } | |
9394133f | 435 | |
f449c9e5 MZ |
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 | } | |
9394133f | 441 | |
f449c9e5 MZ |
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 | } | |
6efeb328 | 449 | } else { |
f449c9e5 MZ |
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) { | |
460 | g_queue_push_head(&conn->secondary_list, spkt); | |
461 | goto pri; | |
462 | } | |
6efeb328 | 463 | } |
f4b61836 | 464 | |
f449c9e5 MZ |
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); | |
479 | g_queue_push_head(&conn->secondary_list, spkt); | |
480 | goto pri; | |
481 | } | |
482 | if (mark == COLO_COMPARE_FREE_SECONDARY) { | |
483 | conn->compare_seq = spkt->seq_end; | |
484 | packet_destroy(spkt, NULL); | |
485 | goto sec; | |
486 | } | |
487 | if (mark == (COLO_COMPARE_FREE_PRIMARY | COLO_COMPARE_FREE_SECONDARY)) { | |
488 | conn->compare_seq = ppkt->seq_end; | |
489 | colo_release_primary_pkt(s, ppkt); | |
490 | packet_destroy(spkt, NULL); | |
491 | goto pri; | |
492 | } | |
493 | } else { | |
494 | g_queue_push_head(&conn->primary_list, ppkt); | |
495 | g_queue_push_head(&conn->secondary_list, spkt); | |
2061c14c | 496 | |
76658541 LS |
497 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
498 | qemu_hexdump((char *)ppkt->data, stderr, | |
499 | "colo-compare ppkt", ppkt->size); | |
500 | qemu_hexdump((char *)spkt->data, stderr, | |
501 | "colo-compare spkt", spkt->size); | |
502 | } | |
f4b61836 | 503 | |
1d09f700 | 504 | colo_compare_inconsistency_notify(s); |
f449c9e5 | 505 | } |
f4b61836 ZC |
506 | } |
507 | ||
f449c9e5 | 508 | |
f4b61836 ZC |
509 | /* |
510 | * Called from the compare thread on the primary | |
511 | * for compare udp packet | |
512 | */ | |
513 | static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt) | |
514 | { | |
9394133f MZ |
515 | uint16_t network_header_length = ppkt->ip->ip_hl << 2; |
516 | uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len; | |
f4b61836 ZC |
517 | |
518 | trace_colo_compare_main("compare udp"); | |
2ad7ca4c | 519 | |
6efeb328 ZC |
520 | /* |
521 | * Because of ppkt and spkt are both in the same connection, | |
522 | * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are | |
523 | * same with spkt. In addition, IP header's Identification is a random | |
524 | * field, we can handle it in IP fragmentation function later. | |
525 | * COLO just concern the response net packet payload from primary guest | |
526 | * and secondary guest are same or not, So we ignored all IP header include | |
527 | * other field like TOS,TTL,IP Checksum. we only need to compare | |
528 | * the ip payload here. | |
529 | */ | |
9394133f MZ |
530 | if (ppkt->size != spkt->size) { |
531 | trace_colo_compare_main("UDP: payload size of packets are different"); | |
532 | return -1; | |
533 | } | |
534 | if (colo_compare_packet_payload(ppkt, spkt, offset, offset, | |
535 | ppkt->size - offset)) { | |
f4b61836 | 536 | trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size); |
f4b61836 | 537 | trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size); |
d87aa138 | 538 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
1723a7f7 ZC |
539 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt", |
540 | ppkt->size); | |
541 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt", | |
542 | spkt->size); | |
543 | } | |
9394133f MZ |
544 | return -1; |
545 | } else { | |
546 | return 0; | |
f4b61836 | 547 | } |
f4b61836 ZC |
548 | } |
549 | ||
550 | /* | |
551 | * Called from the compare thread on the primary | |
552 | * for compare icmp packet | |
553 | */ | |
554 | static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt) | |
555 | { | |
9394133f MZ |
556 | uint16_t network_header_length = ppkt->ip->ip_hl << 2; |
557 | uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len; | |
6efeb328 | 558 | |
f4b61836 | 559 | trace_colo_compare_main("compare icmp"); |
f4b61836 | 560 | |
6efeb328 ZC |
561 | /* |
562 | * Because of ppkt and spkt are both in the same connection, | |
563 | * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are | |
564 | * same with spkt. In addition, IP header's Identification is a random | |
565 | * field, we can handle it in IP fragmentation function later. | |
566 | * COLO just concern the response net packet payload from primary guest | |
567 | * and secondary guest are same or not, So we ignored all IP header include | |
568 | * other field like TOS,TTL,IP Checksum. we only need to compare | |
569 | * the ip payload here. | |
570 | */ | |
9394133f MZ |
571 | if (ppkt->size != spkt->size) { |
572 | trace_colo_compare_main("ICMP: payload size of packets are different"); | |
573 | return -1; | |
574 | } | |
575 | if (colo_compare_packet_payload(ppkt, spkt, offset, offset, | |
576 | ppkt->size - offset)) { | |
f4b61836 ZC |
577 | trace_colo_compare_icmp_miscompare("primary pkt size", |
578 | ppkt->size); | |
f4b61836 ZC |
579 | trace_colo_compare_icmp_miscompare("Secondary pkt size", |
580 | spkt->size); | |
d87aa138 | 581 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
1723a7f7 ZC |
582 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt", |
583 | ppkt->size); | |
584 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt", | |
585 | spkt->size); | |
586 | } | |
f4b61836 ZC |
587 | return -1; |
588 | } else { | |
589 | return 0; | |
590 | } | |
591 | } | |
592 | ||
593 | /* | |
594 | * Called from the compare thread on the primary | |
595 | * for compare other packet | |
596 | */ | |
597 | static int colo_packet_compare_other(Packet *spkt, Packet *ppkt) | |
598 | { | |
9394133f MZ |
599 | uint16_t offset = ppkt->vnet_hdr_len; |
600 | ||
f4b61836 | 601 | trace_colo_compare_main("compare other"); |
d87aa138 | 602 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
e630b2bf ZC |
603 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; |
604 | ||
605 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
606 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
607 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
608 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
609 | ||
610 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
611 | pri_ip_dst, spkt->size, | |
612 | sec_ip_src, sec_ip_dst); | |
613 | } | |
614 | ||
9394133f MZ |
615 | if (ppkt->size != spkt->size) { |
616 | trace_colo_compare_main("Other: payload size of packets are different"); | |
617 | return -1; | |
618 | } | |
619 | return colo_compare_packet_payload(ppkt, spkt, offset, offset, | |
620 | ppkt->size - offset); | |
0682e15b ZC |
621 | } |
622 | ||
623 | static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time) | |
624 | { | |
625 | int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST); | |
626 | ||
627 | if ((now - pkt->creation_ms) > (*check_time)) { | |
628 | trace_colo_old_packet_check_found(pkt->creation_ms); | |
629 | return 0; | |
630 | } else { | |
631 | return 1; | |
632 | } | |
633 | } | |
634 | ||
dccd0313 ZC |
635 | void colo_compare_register_notifier(Notifier *notify) |
636 | { | |
637 | notifier_list_add(&colo_compare_notifiers, notify); | |
638 | } | |
639 | ||
640 | void colo_compare_unregister_notifier(Notifier *notify) | |
641 | { | |
642 | notifier_remove(notify); | |
643 | } | |
644 | ||
d25a7dab | 645 | static int colo_old_packet_check_one_conn(Connection *conn, |
1d09f700 | 646 | CompareState *s) |
0682e15b | 647 | { |
0682e15b | 648 | GList *result = NULL; |
0682e15b ZC |
649 | |
650 | result = g_queue_find_custom(&conn->primary_list, | |
9cc43c94 | 651 | &s->compare_timeout, |
0682e15b ZC |
652 | (GCompareFunc)colo_old_packet_check_one); |
653 | ||
654 | if (result) { | |
61c5f469 | 655 | /* Do checkpoint will flush old packet */ |
1d09f700 | 656 | colo_compare_inconsistency_notify(s); |
d25a7dab | 657 | return 0; |
0682e15b | 658 | } |
d25a7dab ZC |
659 | |
660 | return 1; | |
0682e15b ZC |
661 | } |
662 | ||
663 | /* | |
664 | * Look for old packets that the secondary hasn't matched, | |
665 | * if we have some then we have to checkpoint to wake | |
666 | * the secondary up. | |
667 | */ | |
668 | static void colo_old_packet_check(void *opaque) | |
669 | { | |
670 | CompareState *s = opaque; | |
671 | ||
d25a7dab ZC |
672 | /* |
673 | * If we find one old packet, stop finding job and notify | |
674 | * COLO frame do checkpoint. | |
675 | */ | |
1d09f700 | 676 | g_queue_find_custom(&s->conn_list, s, |
d25a7dab | 677 | (GCompareFunc)colo_old_packet_check_one_conn); |
0682e15b ZC |
678 | } |
679 | ||
f449c9e5 MZ |
680 | static void colo_compare_packet(CompareState *s, Connection *conn, |
681 | int (*HandlePacket)(Packet *spkt, | |
682 | Packet *ppkt)) | |
0682e15b | 683 | { |
0682e15b ZC |
684 | Packet *pkt = NULL; |
685 | GList *result = NULL; | |
0682e15b ZC |
686 | |
687 | while (!g_queue_is_empty(&conn->primary_list) && | |
688 | !g_queue_is_empty(&conn->secondary_list)) { | |
626bba98 | 689 | pkt = g_queue_pop_head(&conn->primary_list); |
f449c9e5 MZ |
690 | result = g_queue_find_custom(&conn->secondary_list, |
691 | pkt, (GCompareFunc)HandlePacket); | |
0682e15b ZC |
692 | |
693 | if (result) { | |
f449c9e5 | 694 | colo_release_primary_pkt(s, pkt); |
0682e15b | 695 | g_queue_remove(&conn->secondary_list, result->data); |
0682e15b ZC |
696 | } else { |
697 | /* | |
698 | * If one packet arrive late, the secondary_list or | |
699 | * primary_list will be empty, so we can't compare it | |
dccd0313 ZC |
700 | * until next comparison. If the packets in the list are |
701 | * timeout, it will trigger a checkpoint request. | |
0682e15b ZC |
702 | */ |
703 | trace_colo_compare_main("packet different"); | |
626bba98 | 704 | g_queue_push_head(&conn->primary_list, pkt); |
1d09f700 ZC |
705 | |
706 | colo_compare_inconsistency_notify(s); | |
0682e15b ZC |
707 | break; |
708 | } | |
709 | } | |
710 | } | |
711 | ||
f449c9e5 MZ |
712 | /* |
713 | * Called from the compare thread on the primary | |
714 | * for compare packet with secondary list of the | |
715 | * specified connection when a new packet was | |
716 | * queued to it. | |
717 | */ | |
718 | static void colo_compare_connection(void *opaque, void *user_data) | |
719 | { | |
720 | CompareState *s = user_data; | |
721 | Connection *conn = opaque; | |
722 | ||
723 | switch (conn->ip_proto) { | |
724 | case IPPROTO_TCP: | |
725 | colo_compare_tcp(s, conn); | |
726 | break; | |
727 | case IPPROTO_UDP: | |
728 | colo_compare_packet(s, conn, colo_packet_compare_udp); | |
729 | break; | |
730 | case IPPROTO_ICMP: | |
731 | colo_compare_packet(s, conn, colo_packet_compare_icmp); | |
732 | break; | |
733 | default: | |
734 | colo_compare_packet(s, conn, colo_packet_compare_other); | |
735 | break; | |
736 | } | |
737 | } | |
738 | ||
9c55fe94 | 739 | static void coroutine_fn _compare_chr_send(void *opaque) |
59509ec1 | 740 | { |
9c55fe94 LS |
741 | SendCo *sendco = opaque; |
742 | CompareState *s = sendco->s; | |
59509ec1 | 743 | int ret = 0; |
59509ec1 | 744 | |
9c55fe94 LS |
745 | while (!g_queue_is_empty(&sendco->send_list)) { |
746 | SendEntry *entry = g_queue_pop_tail(&sendco->send_list); | |
747 | uint32_t len = htonl(entry->size); | |
59509ec1 | 748 | |
9c55fe94 | 749 | ret = qemu_chr_fe_write_all(sendco->chr, (uint8_t *)&len, sizeof(len)); |
30685c00 | 750 | |
9c55fe94 LS |
751 | if (ret != sizeof(len)) { |
752 | g_free(entry->buf); | |
753 | g_slice_free(SendEntry, entry); | |
754 | goto err; | |
755 | } | |
59509ec1 | 756 | |
9c55fe94 LS |
757 | if (!sendco->notify_remote_frame && s->vnet_hdr) { |
758 | /* | |
759 | * We send vnet header len make other module(like filter-redirector) | |
760 | * know how to parse net packet correctly. | |
761 | */ | |
762 | len = htonl(entry->vnet_hdr_len); | |
30685c00 | 763 | |
9c55fe94 | 764 | ret = qemu_chr_fe_write_all(sendco->chr, |
30685c00 ZC |
765 | (uint8_t *)&len, |
766 | sizeof(len)); | |
9c55fe94 LS |
767 | |
768 | if (ret != sizeof(len)) { | |
769 | g_free(entry->buf); | |
770 | g_slice_free(SendEntry, entry); | |
771 | goto err; | |
772 | } | |
30685c00 ZC |
773 | } |
774 | ||
9c55fe94 LS |
775 | ret = qemu_chr_fe_write_all(sendco->chr, |
776 | (uint8_t *)entry->buf, | |
777 | entry->size); | |
778 | ||
779 | if (ret != entry->size) { | |
780 | g_free(entry->buf); | |
781 | g_slice_free(SendEntry, entry); | |
aa3a7032 ZC |
782 | goto err; |
783 | } | |
9c55fe94 LS |
784 | |
785 | g_free(entry->buf); | |
786 | g_slice_free(SendEntry, entry); | |
aa3a7032 ZC |
787 | } |
788 | ||
9c55fe94 LS |
789 | sendco->ret = 0; |
790 | goto out; | |
791 | ||
792 | err: | |
793 | while (!g_queue_is_empty(&sendco->send_list)) { | |
794 | SendEntry *entry = g_queue_pop_tail(&sendco->send_list); | |
795 | g_free(entry->buf); | |
796 | g_slice_free(SendEntry, entry); | |
797 | } | |
798 | sendco->ret = ret < 0 ? ret : -EIO; | |
799 | out: | |
800 | sendco->co = NULL; | |
801 | sendco->done = true; | |
802 | aio_wait_kick(); | |
803 | } | |
804 | ||
805 | static int compare_chr_send(CompareState *s, | |
806 | uint8_t *buf, | |
807 | uint32_t size, | |
808 | uint32_t vnet_hdr_len, | |
809 | bool notify_remote_frame, | |
810 | bool zero_copy) | |
811 | { | |
812 | SendCo *sendco; | |
813 | SendEntry *entry; | |
814 | ||
30685c00 | 815 | if (notify_remote_frame) { |
9c55fe94 | 816 | sendco = &s->notify_sendco; |
30685c00 | 817 | } else { |
9c55fe94 | 818 | sendco = &s->out_sendco; |
30685c00 ZC |
819 | } |
820 | ||
9c55fe94 LS |
821 | if (!size) { |
822 | return 0; | |
59509ec1 ZC |
823 | } |
824 | ||
9c55fe94 LS |
825 | entry = g_slice_new(SendEntry); |
826 | entry->size = size; | |
827 | entry->vnet_hdr_len = vnet_hdr_len; | |
828 | if (zero_copy) { | |
829 | entry->buf = buf; | |
830 | } else { | |
831 | entry->buf = g_malloc(size); | |
832 | memcpy(entry->buf, buf, size); | |
833 | } | |
834 | g_queue_push_head(&sendco->send_list, entry); | |
835 | ||
836 | if (sendco->done) { | |
837 | sendco->co = qemu_coroutine_create(_compare_chr_send, sendco); | |
838 | sendco->done = false; | |
839 | qemu_coroutine_enter(sendco->co); | |
840 | if (sendco->done) { | |
841 | /* report early errors */ | |
842 | return sendco->ret; | |
843 | } | |
844 | } | |
59509ec1 | 845 | |
9c55fe94 LS |
846 | /* assume success */ |
847 | return 0; | |
59509ec1 ZC |
848 | } |
849 | ||
0682e15b ZC |
850 | static int compare_chr_can_read(void *opaque) |
851 | { | |
852 | return COMPARE_READ_LEN_MAX; | |
853 | } | |
854 | ||
855 | /* | |
856 | * Called from the main thread on the primary for packets | |
857 | * arriving over the socket from the primary. | |
858 | */ | |
859 | static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size) | |
860 | { | |
861 | CompareState *s = COLO_COMPARE(opaque); | |
862 | int ret; | |
863 | ||
864 | ret = net_fill_rstate(&s->pri_rs, buf, size); | |
865 | if (ret == -1) { | |
81517ba3 | 866 | qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL, |
39ab61c6 | 867 | NULL, NULL, true); |
0682e15b ZC |
868 | error_report("colo-compare primary_in error"); |
869 | } | |
870 | } | |
871 | ||
872 | /* | |
873 | * Called from the main thread on the primary for packets | |
874 | * arriving over the socket from the secondary. | |
875 | */ | |
876 | static void compare_sec_chr_in(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->sec_rs, buf, size); | |
882 | if (ret == -1) { | |
81517ba3 | 883 | qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL, |
39ab61c6 | 884 | NULL, NULL, true); |
0682e15b ZC |
885 | error_report("colo-compare secondary_in error"); |
886 | } | |
887 | } | |
888 | ||
13025fee ZC |
889 | static void compare_notify_chr(void *opaque, const uint8_t *buf, int size) |
890 | { | |
891 | CompareState *s = COLO_COMPARE(opaque); | |
892 | int ret; | |
893 | ||
894 | ret = net_fill_rstate(&s->notify_rs, buf, size); | |
895 | if (ret == -1) { | |
896 | qemu_chr_fe_set_handlers(&s->chr_notify_dev, NULL, NULL, NULL, NULL, | |
897 | NULL, NULL, true); | |
898 | error_report("colo-compare notify_dev error"); | |
899 | } | |
900 | } | |
901 | ||
66d2a242 HZ |
902 | /* |
903 | * Check old packet regularly so it can watch for any packets | |
904 | * that the secondary hasn't produced equivalents of. | |
905 | */ | |
dd321ecf | 906 | static void check_old_packet_regular(void *opaque) |
66d2a242 HZ |
907 | { |
908 | CompareState *s = opaque; | |
909 | ||
910 | /* if have old packet we will notify checkpoint */ | |
911 | colo_old_packet_check(s); | |
dd321ecf | 912 | timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + |
cca35ac4 | 913 | s->expired_scan_cycle); |
dd321ecf WY |
914 | } |
915 | ||
0ffcece3 ZC |
916 | /* Public API, Used for COLO frame to notify compare event */ |
917 | void colo_notify_compares_event(void *opaque, int event, Error **errp) | |
918 | { | |
919 | CompareState *s; | |
45942b79 LS |
920 | qemu_mutex_lock(&colo_compare_mutex); |
921 | ||
922 | if (!colo_compare_active) { | |
923 | qemu_mutex_unlock(&colo_compare_mutex); | |
924 | return; | |
925 | } | |
0ffcece3 ZC |
926 | |
927 | qemu_mutex_lock(&event_mtx); | |
928 | QTAILQ_FOREACH(s, &net_compares, next) { | |
929 | s->event = event; | |
930 | qemu_bh_schedule(s->event_bh); | |
931 | event_unhandled_count++; | |
932 | } | |
933 | /* Wait all compare threads to finish handling this event */ | |
934 | while (event_unhandled_count > 0) { | |
935 | qemu_cond_wait(&event_complete_cond, &event_mtx); | |
936 | } | |
937 | ||
938 | qemu_mutex_unlock(&event_mtx); | |
45942b79 | 939 | qemu_mutex_unlock(&colo_compare_mutex); |
0ffcece3 ZC |
940 | } |
941 | ||
dd321ecf WY |
942 | static void colo_compare_timer_init(CompareState *s) |
943 | { | |
944 | AioContext *ctx = iothread_get_aio_context(s->iothread); | |
66d2a242 | 945 | |
dd321ecf WY |
946 | s->packet_check_timer = aio_timer_new(ctx, QEMU_CLOCK_VIRTUAL, |
947 | SCALE_MS, check_old_packet_regular, | |
948 | s); | |
949 | timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + | |
cca35ac4 | 950 | s->expired_scan_cycle); |
66d2a242 HZ |
951 | } |
952 | ||
dd321ecf | 953 | static void colo_compare_timer_del(CompareState *s) |
0682e15b | 954 | { |
dd321ecf WY |
955 | if (s->packet_check_timer) { |
956 | timer_del(s->packet_check_timer); | |
957 | timer_free(s->packet_check_timer); | |
958 | s->packet_check_timer = NULL; | |
959 | } | |
960 | } | |
0682e15b | 961 | |
0ffcece3 ZC |
962 | static void colo_flush_packets(void *opaque, void *user_data); |
963 | ||
964 | static void colo_compare_handle_event(void *opaque) | |
965 | { | |
966 | CompareState *s = opaque; | |
967 | ||
968 | switch (s->event) { | |
969 | case COLO_EVENT_CHECKPOINT: | |
970 | g_queue_foreach(&s->conn_list, colo_flush_packets, s); | |
971 | break; | |
972 | case COLO_EVENT_FAILOVER: | |
973 | break; | |
974 | default: | |
975 | break; | |
976 | } | |
977 | ||
0ffcece3 | 978 | qemu_mutex_lock(&event_mtx); |
78e4f446 | 979 | assert(event_unhandled_count > 0); |
0ffcece3 ZC |
980 | event_unhandled_count--; |
981 | qemu_cond_broadcast(&event_complete_cond); | |
982 | qemu_mutex_unlock(&event_mtx); | |
983 | } | |
984 | ||
dd321ecf WY |
985 | static void colo_compare_iothread(CompareState *s) |
986 | { | |
5893c738 | 987 | AioContext *ctx = iothread_get_aio_context(s->iothread); |
dd321ecf WY |
988 | object_ref(OBJECT(s->iothread)); |
989 | s->worker_context = iothread_get_g_main_context(s->iothread); | |
0682e15b | 990 | |
5345fdb4 | 991 | qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read, |
81517ba3 AN |
992 | compare_pri_chr_in, NULL, NULL, |
993 | s, s->worker_context, true); | |
5345fdb4 | 994 | qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read, |
81517ba3 AN |
995 | compare_sec_chr_in, NULL, NULL, |
996 | s, s->worker_context, true); | |
13025fee ZC |
997 | if (s->notify_dev) { |
998 | qemu_chr_fe_set_handlers(&s->chr_notify_dev, compare_chr_can_read, | |
999 | compare_notify_chr, NULL, NULL, | |
1000 | s, s->worker_context, true); | |
1001 | } | |
0682e15b | 1002 | |
dd321ecf | 1003 | colo_compare_timer_init(s); |
5893c738 | 1004 | s->event_bh = aio_bh_new(ctx, colo_compare_handle_event, s); |
0682e15b ZC |
1005 | } |
1006 | ||
7dce4e6f ZC |
1007 | static char *compare_get_pri_indev(Object *obj, Error **errp) |
1008 | { | |
1009 | CompareState *s = COLO_COMPARE(obj); | |
1010 | ||
1011 | return g_strdup(s->pri_indev); | |
1012 | } | |
1013 | ||
1014 | static void compare_set_pri_indev(Object *obj, const char *value, Error **errp) | |
1015 | { | |
1016 | CompareState *s = COLO_COMPARE(obj); | |
1017 | ||
1018 | g_free(s->pri_indev); | |
1019 | s->pri_indev = g_strdup(value); | |
1020 | } | |
1021 | ||
1022 | static char *compare_get_sec_indev(Object *obj, Error **errp) | |
1023 | { | |
1024 | CompareState *s = COLO_COMPARE(obj); | |
1025 | ||
1026 | return g_strdup(s->sec_indev); | |
1027 | } | |
1028 | ||
1029 | static void compare_set_sec_indev(Object *obj, const char *value, Error **errp) | |
1030 | { | |
1031 | CompareState *s = COLO_COMPARE(obj); | |
1032 | ||
1033 | g_free(s->sec_indev); | |
1034 | s->sec_indev = g_strdup(value); | |
1035 | } | |
1036 | ||
1037 | static char *compare_get_outdev(Object *obj, Error **errp) | |
1038 | { | |
1039 | CompareState *s = COLO_COMPARE(obj); | |
1040 | ||
1041 | return g_strdup(s->outdev); | |
1042 | } | |
1043 | ||
1044 | static void compare_set_outdev(Object *obj, const char *value, Error **errp) | |
1045 | { | |
1046 | CompareState *s = COLO_COMPARE(obj); | |
1047 | ||
1048 | g_free(s->outdev); | |
1049 | s->outdev = g_strdup(value); | |
1050 | } | |
1051 | ||
aa3a7032 ZC |
1052 | static bool compare_get_vnet_hdr(Object *obj, Error **errp) |
1053 | { | |
1054 | CompareState *s = COLO_COMPARE(obj); | |
1055 | ||
1056 | return s->vnet_hdr; | |
1057 | } | |
1058 | ||
1059 | static void compare_set_vnet_hdr(Object *obj, | |
1060 | bool value, | |
1061 | Error **errp) | |
1062 | { | |
1063 | CompareState *s = COLO_COMPARE(obj); | |
1064 | ||
1065 | s->vnet_hdr = value; | |
1066 | } | |
1067 | ||
cf6af766 ZC |
1068 | static char *compare_get_notify_dev(Object *obj, Error **errp) |
1069 | { | |
1070 | CompareState *s = COLO_COMPARE(obj); | |
1071 | ||
1072 | return g_strdup(s->notify_dev); | |
1073 | } | |
1074 | ||
1075 | static void compare_set_notify_dev(Object *obj, const char *value, Error **errp) | |
1076 | { | |
1077 | CompareState *s = COLO_COMPARE(obj); | |
1078 | ||
1079 | g_free(s->notify_dev); | |
1080 | s->notify_dev = g_strdup(value); | |
1081 | } | |
1082 | ||
9cc43c94 ZC |
1083 | static void compare_get_timeout(Object *obj, Visitor *v, |
1084 | const char *name, void *opaque, | |
1085 | Error **errp) | |
1086 | { | |
1087 | CompareState *s = COLO_COMPARE(obj); | |
1088 | uint32_t value = s->compare_timeout; | |
1089 | ||
1090 | visit_type_uint32(v, name, &value, errp); | |
1091 | } | |
1092 | ||
1093 | static void compare_set_timeout(Object *obj, Visitor *v, | |
1094 | const char *name, void *opaque, | |
1095 | Error **errp) | |
1096 | { | |
1097 | CompareState *s = COLO_COMPARE(obj); | |
9cc43c94 ZC |
1098 | uint32_t value; |
1099 | ||
668f62ec | 1100 | if (!visit_type_uint32(v, name, &value, errp)) { |
dcfe4805 | 1101 | return; |
9cc43c94 ZC |
1102 | } |
1103 | if (!value) { | |
dcfe4805 | 1104 | error_setg(errp, "Property '%s.%s' requires a positive value", |
9cc43c94 | 1105 | object_get_typename(obj), name); |
dcfe4805 | 1106 | return; |
9cc43c94 ZC |
1107 | } |
1108 | s->compare_timeout = value; | |
9cc43c94 ZC |
1109 | } |
1110 | ||
cca35ac4 ZC |
1111 | static void compare_get_expired_scan_cycle(Object *obj, Visitor *v, |
1112 | const char *name, void *opaque, | |
1113 | Error **errp) | |
1114 | { | |
1115 | CompareState *s = COLO_COMPARE(obj); | |
1116 | uint32_t value = s->expired_scan_cycle; | |
1117 | ||
1118 | visit_type_uint32(v, name, &value, errp); | |
1119 | } | |
1120 | ||
1121 | static void compare_set_expired_scan_cycle(Object *obj, Visitor *v, | |
1122 | const char *name, void *opaque, | |
1123 | Error **errp) | |
1124 | { | |
1125 | CompareState *s = COLO_COMPARE(obj); | |
cca35ac4 ZC |
1126 | uint32_t value; |
1127 | ||
668f62ec | 1128 | if (!visit_type_uint32(v, name, &value, errp)) { |
dcfe4805 | 1129 | return; |
cca35ac4 ZC |
1130 | } |
1131 | if (!value) { | |
dcfe4805 | 1132 | error_setg(errp, "Property '%s.%s' requires a positive value", |
cca35ac4 | 1133 | object_get_typename(obj), name); |
dcfe4805 | 1134 | return; |
cca35ac4 ZC |
1135 | } |
1136 | s->expired_scan_cycle = value; | |
cca35ac4 ZC |
1137 | } |
1138 | ||
a2e5cb7a ZC |
1139 | static void get_max_queue_size(Object *obj, Visitor *v, |
1140 | const char *name, void *opaque, | |
1141 | Error **errp) | |
1142 | { | |
1143 | uint32_t value = max_queue_size; | |
1144 | ||
1145 | visit_type_uint32(v, name, &value, errp); | |
1146 | } | |
1147 | ||
1148 | static void set_max_queue_size(Object *obj, Visitor *v, | |
1149 | const char *name, void *opaque, | |
1150 | Error **errp) | |
1151 | { | |
1152 | Error *local_err = NULL; | |
1153 | uint32_t value; | |
1154 | ||
1155 | visit_type_uint32(v, name, &value, &local_err); | |
1156 | if (local_err) { | |
1157 | goto out; | |
1158 | } | |
1159 | if (!value) { | |
1160 | error_setg(&local_err, "Property '%s.%s' requires a positive value", | |
1161 | object_get_typename(obj), name); | |
1162 | goto out; | |
1163 | } | |
1164 | max_queue_size = value; | |
1165 | ||
1166 | out: | |
1167 | error_propagate(errp, local_err); | |
1168 | } | |
1169 | ||
7dce4e6f ZC |
1170 | static void compare_pri_rs_finalize(SocketReadState *pri_rs) |
1171 | { | |
59509ec1 | 1172 | CompareState *s = container_of(pri_rs, CompareState, pri_rs); |
8ec14402 | 1173 | Connection *conn = NULL; |
59509ec1 | 1174 | |
8ec14402 | 1175 | if (packet_enqueue(s, PRIMARY_IN, &conn)) { |
59509ec1 | 1176 | trace_colo_compare_main("primary: unsupported packet in"); |
aa3a7032 ZC |
1177 | compare_chr_send(s, |
1178 | pri_rs->buf, | |
1179 | pri_rs->packet_len, | |
30685c00 | 1180 | pri_rs->vnet_hdr_len, |
9c55fe94 | 1181 | false, |
30685c00 | 1182 | false); |
0682e15b | 1183 | } else { |
3463218c | 1184 | /* compare packet in the specified connection */ |
8ec14402 | 1185 | colo_compare_connection(conn, s); |
59509ec1 | 1186 | } |
7dce4e6f ZC |
1187 | } |
1188 | ||
1189 | static void compare_sec_rs_finalize(SocketReadState *sec_rs) | |
1190 | { | |
59509ec1 | 1191 | CompareState *s = container_of(sec_rs, CompareState, sec_rs); |
8ec14402 | 1192 | Connection *conn = NULL; |
59509ec1 | 1193 | |
8ec14402 | 1194 | if (packet_enqueue(s, SECONDARY_IN, &conn)) { |
59509ec1 | 1195 | trace_colo_compare_main("secondary: unsupported packet in"); |
0682e15b | 1196 | } else { |
3463218c | 1197 | /* compare packet in the specified connection */ |
8ec14402 | 1198 | colo_compare_connection(conn, s); |
59509ec1 | 1199 | } |
7dce4e6f ZC |
1200 | } |
1201 | ||
13025fee ZC |
1202 | static void compare_notify_rs_finalize(SocketReadState *notify_rs) |
1203 | { | |
1d09f700 ZC |
1204 | CompareState *s = container_of(notify_rs, CompareState, notify_rs); |
1205 | ||
f77bed14 | 1206 | const char msg[] = "COLO_COMPARE_GET_XEN_INIT"; |
1d09f700 ZC |
1207 | int ret; |
1208 | ||
f77bed14 ZC |
1209 | if (packet_matches_str("COLO_USERSPACE_PROXY_INIT", |
1210 | notify_rs->buf, | |
1211 | notify_rs->packet_len)) { | |
9c55fe94 | 1212 | ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true, false); |
1d09f700 ZC |
1213 | if (ret < 0) { |
1214 | error_report("Notify Xen COLO-frame INIT failed"); | |
1215 | } | |
f77bed14 ZC |
1216 | } else if (packet_matches_str("COLO_CHECKPOINT", |
1217 | notify_rs->buf, | |
1218 | notify_rs->packet_len)) { | |
1d09f700 ZC |
1219 | /* colo-compare do checkpoint, flush pri packet and remove sec packet */ |
1220 | g_queue_foreach(&s->conn_list, colo_flush_packets, s); | |
f77bed14 ZC |
1221 | } else { |
1222 | error_report("COLO compare got unsupported instruction"); | |
1d09f700 | 1223 | } |
13025fee | 1224 | } |
7dce4e6f ZC |
1225 | |
1226 | /* | |
1227 | * Return 0 is success. | |
1228 | * Return 1 is failed. | |
1229 | */ | |
0ec7b3e7 | 1230 | static int find_and_check_chardev(Chardev **chr, |
7dce4e6f ZC |
1231 | char *chr_name, |
1232 | Error **errp) | |
1233 | { | |
7dce4e6f ZC |
1234 | *chr = qemu_chr_find(chr_name); |
1235 | if (*chr == NULL) { | |
1236 | error_setg(errp, "Device '%s' not found", | |
1237 | chr_name); | |
1238 | return 1; | |
1239 | } | |
1240 | ||
0a73336d DB |
1241 | if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) { |
1242 | error_setg(errp, "chardev \"%s\" is not reconnectable", | |
7dce4e6f ZC |
1243 | chr_name); |
1244 | return 1; | |
1245 | } | |
fbf3cc3a | 1246 | |
269d25cd MAL |
1247 | if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_GCONTEXT)) { |
1248 | error_setg(errp, "chardev \"%s\" cannot switch context", | |
1249 | chr_name); | |
1250 | return 1; | |
1251 | } | |
1252 | ||
7dce4e6f ZC |
1253 | return 0; |
1254 | } | |
1255 | ||
1256 | /* | |
1257 | * Called from the main thread on the primary | |
1258 | * to setup colo-compare. | |
1259 | */ | |
1260 | static void colo_compare_complete(UserCreatable *uc, Error **errp) | |
1261 | { | |
1262 | CompareState *s = COLO_COMPARE(uc); | |
0ec7b3e7 | 1263 | Chardev *chr; |
7dce4e6f | 1264 | |
dd321ecf | 1265 | if (!s->pri_indev || !s->sec_indev || !s->outdev || !s->iothread) { |
7dce4e6f | 1266 | error_setg(errp, "colo compare needs 'primary_in' ," |
dd321ecf | 1267 | "'secondary_in','outdev','iothread' property set"); |
7dce4e6f ZC |
1268 | return; |
1269 | } else if (!strcmp(s->pri_indev, s->outdev) || | |
1270 | !strcmp(s->sec_indev, s->outdev) || | |
1271 | !strcmp(s->pri_indev, s->sec_indev)) { | |
1272 | error_setg(errp, "'indev' and 'outdev' could not be same " | |
1273 | "for compare module"); | |
1274 | return; | |
1275 | } | |
1276 | ||
9cc43c94 ZC |
1277 | if (!s->compare_timeout) { |
1278 | /* Set default value to 3000 MS */ | |
1279 | s->compare_timeout = DEFAULT_TIME_OUT_MS; | |
1280 | } | |
1281 | ||
cca35ac4 ZC |
1282 | if (!s->expired_scan_cycle) { |
1283 | /* Set default value to 3000 MS */ | |
1284 | s->expired_scan_cycle = REGULAR_PACKET_CHECK_MS; | |
1285 | } | |
1286 | ||
a2e5cb7a ZC |
1287 | if (!max_queue_size) { |
1288 | /* Set default queue size to 1024 */ | |
1289 | max_queue_size = MAX_QUEUE_SIZE; | |
1290 | } | |
1291 | ||
5345fdb4 MAL |
1292 | if (find_and_check_chardev(&chr, s->pri_indev, errp) || |
1293 | !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) { | |
7dce4e6f ZC |
1294 | return; |
1295 | } | |
1296 | ||
5345fdb4 MAL |
1297 | if (find_and_check_chardev(&chr, s->sec_indev, errp) || |
1298 | !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) { | |
7dce4e6f ZC |
1299 | return; |
1300 | } | |
1301 | ||
5345fdb4 MAL |
1302 | if (find_and_check_chardev(&chr, s->outdev, errp) || |
1303 | !qemu_chr_fe_init(&s->chr_out, chr, errp)) { | |
7dce4e6f ZC |
1304 | return; |
1305 | } | |
1306 | ||
aa3a7032 ZC |
1307 | net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr); |
1308 | net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr); | |
7dce4e6f | 1309 | |
13025fee ZC |
1310 | /* Try to enable remote notify chardev, currently just for Xen COLO */ |
1311 | if (s->notify_dev) { | |
1312 | if (find_and_check_chardev(&chr, s->notify_dev, errp) || | |
1313 | !qemu_chr_fe_init(&s->chr_notify_dev, chr, errp)) { | |
1314 | return; | |
1315 | } | |
1316 | ||
1317 | net_socket_rs_init(&s->notify_rs, compare_notify_rs_finalize, | |
1318 | s->vnet_hdr); | |
1319 | } | |
1320 | ||
9c55fe94 LS |
1321 | s->out_sendco.s = s; |
1322 | s->out_sendco.chr = &s->chr_out; | |
1323 | s->out_sendco.notify_remote_frame = false; | |
1324 | s->out_sendco.done = true; | |
1325 | g_queue_init(&s->out_sendco.send_list); | |
1326 | ||
1327 | if (s->notify_dev) { | |
1328 | s->notify_sendco.s = s; | |
1329 | s->notify_sendco.chr = &s->chr_notify_dev; | |
1330 | s->notify_sendco.notify_remote_frame = true; | |
1331 | s->notify_sendco.done = true; | |
1332 | g_queue_init(&s->notify_sendco.send_list); | |
1333 | } | |
1334 | ||
b6540d40 ZC |
1335 | g_queue_init(&s->conn_list); |
1336 | ||
1337 | s->connection_track_table = g_hash_table_new_full(connection_key_hash, | |
1338 | connection_key_equal, | |
1339 | g_free, | |
1340 | connection_destroy); | |
59509ec1 | 1341 | |
dd321ecf | 1342 | colo_compare_iothread(s); |
5bd57eba LS |
1343 | |
1344 | qemu_mutex_lock(&colo_compare_mutex); | |
1345 | if (!colo_compare_active) { | |
1346 | qemu_mutex_init(&event_mtx); | |
1347 | qemu_cond_init(&event_complete_cond); | |
1348 | colo_compare_active = true; | |
1349 | } | |
1350 | QTAILQ_INSERT_TAIL(&net_compares, s, next); | |
1351 | qemu_mutex_unlock(&colo_compare_mutex); | |
1352 | ||
7dce4e6f ZC |
1353 | return; |
1354 | } | |
1355 | ||
dfd917a9 HZ |
1356 | static void colo_flush_packets(void *opaque, void *user_data) |
1357 | { | |
1358 | CompareState *s = user_data; | |
1359 | Connection *conn = opaque; | |
1360 | Packet *pkt = NULL; | |
1361 | ||
1362 | while (!g_queue_is_empty(&conn->primary_list)) { | |
1363 | pkt = g_queue_pop_head(&conn->primary_list); | |
aa3a7032 ZC |
1364 | compare_chr_send(s, |
1365 | pkt->data, | |
1366 | pkt->size, | |
30685c00 | 1367 | pkt->vnet_hdr_len, |
9c55fe94 LS |
1368 | false, |
1369 | true); | |
1370 | packet_destroy_partial(pkt, NULL); | |
dfd917a9 HZ |
1371 | } |
1372 | while (!g_queue_is_empty(&conn->secondary_list)) { | |
1373 | pkt = g_queue_pop_head(&conn->secondary_list); | |
1374 | packet_destroy(pkt, NULL); | |
1375 | } | |
1376 | } | |
1377 | ||
7dce4e6f ZC |
1378 | static void colo_compare_class_init(ObjectClass *oc, void *data) |
1379 | { | |
1380 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); | |
1381 | ||
1382 | ucc->complete = colo_compare_complete; | |
1383 | } | |
1384 | ||
1385 | static void colo_compare_init(Object *obj) | |
1386 | { | |
aa3a7032 ZC |
1387 | CompareState *s = COLO_COMPARE(obj); |
1388 | ||
7dce4e6f | 1389 | object_property_add_str(obj, "primary_in", |
d2623129 | 1390 | compare_get_pri_indev, compare_set_pri_indev); |
7dce4e6f | 1391 | object_property_add_str(obj, "secondary_in", |
d2623129 | 1392 | compare_get_sec_indev, compare_set_sec_indev); |
7dce4e6f | 1393 | object_property_add_str(obj, "outdev", |
d2623129 | 1394 | compare_get_outdev, compare_set_outdev); |
dd321ecf WY |
1395 | object_property_add_link(obj, "iothread", TYPE_IOTHREAD, |
1396 | (Object **)&s->iothread, | |
1397 | object_property_allow_set_link, | |
d2623129 | 1398 | OBJ_PROP_LINK_STRONG); |
cf6af766 ZC |
1399 | /* This parameter just for Xen COLO */ |
1400 | object_property_add_str(obj, "notify_dev", | |
d2623129 | 1401 | compare_get_notify_dev, compare_set_notify_dev); |
aa3a7032 | 1402 | |
9cc43c94 ZC |
1403 | object_property_add(obj, "compare_timeout", "uint32", |
1404 | compare_get_timeout, | |
d2623129 | 1405 | compare_set_timeout, NULL, NULL); |
9cc43c94 | 1406 | |
cca35ac4 ZC |
1407 | object_property_add(obj, "expired_scan_cycle", "uint32", |
1408 | compare_get_expired_scan_cycle, | |
d2623129 | 1409 | compare_set_expired_scan_cycle, NULL, NULL); |
cca35ac4 | 1410 | |
a2e5cb7a ZC |
1411 | object_property_add(obj, "max_queue_size", "uint32", |
1412 | get_max_queue_size, | |
1413 | set_max_queue_size, NULL, NULL); | |
1414 | ||
aa3a7032 ZC |
1415 | s->vnet_hdr = false; |
1416 | object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr, | |
d2623129 | 1417 | compare_set_vnet_hdr); |
7dce4e6f ZC |
1418 | } |
1419 | ||
1420 | static void colo_compare_finalize(Object *obj) | |
1421 | { | |
1422 | CompareState *s = COLO_COMPARE(obj); | |
0ffcece3 | 1423 | CompareState *tmp = NULL; |
7dce4e6f | 1424 | |
45942b79 | 1425 | qemu_mutex_lock(&colo_compare_mutex); |
0ffcece3 ZC |
1426 | QTAILQ_FOREACH(tmp, &net_compares, next) { |
1427 | if (tmp == s) { | |
1428 | QTAILQ_REMOVE(&net_compares, s, next); | |
1429 | break; | |
1430 | } | |
1431 | } | |
45942b79 LS |
1432 | if (QTAILQ_EMPTY(&net_compares)) { |
1433 | colo_compare_active = false; | |
1434 | qemu_mutex_destroy(&event_mtx); | |
1435 | qemu_cond_destroy(&event_complete_cond); | |
1436 | } | |
1437 | qemu_mutex_unlock(&colo_compare_mutex); | |
0ffcece3 | 1438 | |
5bd57eba LS |
1439 | qemu_chr_fe_deinit(&s->chr_pri_in, false); |
1440 | qemu_chr_fe_deinit(&s->chr_sec_in, false); | |
1441 | qemu_chr_fe_deinit(&s->chr_out, false); | |
1442 | if (s->notify_dev) { | |
1443 | qemu_chr_fe_deinit(&s->chr_notify_dev, false); | |
1444 | } | |
1445 | ||
f81cddfe | 1446 | colo_compare_timer_del(s); |
5bd57eba LS |
1447 | |
1448 | qemu_bh_delete(s->event_bh); | |
1449 | ||
9c55fe94 LS |
1450 | AioContext *ctx = iothread_get_aio_context(s->iothread); |
1451 | aio_context_acquire(ctx); | |
1452 | AIO_WAIT_WHILE(ctx, !s->out_sendco.done); | |
1453 | if (s->notify_dev) { | |
1454 | AIO_WAIT_WHILE(ctx, !s->notify_sendco.done); | |
1455 | } | |
1456 | aio_context_release(ctx); | |
1457 | ||
dfd917a9 HZ |
1458 | /* Release all unhandled packets after compare thead exited */ |
1459 | g_queue_foreach(&s->conn_list, colo_flush_packets, s); | |
9c55fe94 | 1460 | AIO_WAIT_WHILE(NULL, !s->out_sendco.done); |
dfd917a9 | 1461 | |
727c2d76 | 1462 | g_queue_clear(&s->conn_list); |
9c55fe94 LS |
1463 | g_queue_clear(&s->out_sendco.send_list); |
1464 | if (s->notify_dev) { | |
1465 | g_queue_clear(&s->notify_sendco.send_list); | |
1466 | } | |
0682e15b | 1467 | |
dd321ecf WY |
1468 | if (s->connection_track_table) { |
1469 | g_hash_table_destroy(s->connection_track_table); | |
1470 | } | |
1471 | ||
f81cddfe | 1472 | object_unref(OBJECT(s->iothread)); |
0ffcece3 | 1473 | |
7dce4e6f ZC |
1474 | g_free(s->pri_indev); |
1475 | g_free(s->sec_indev); | |
1476 | g_free(s->outdev); | |
cf6af766 | 1477 | g_free(s->notify_dev); |
7dce4e6f ZC |
1478 | } |
1479 | ||
45942b79 LS |
1480 | static void __attribute__((__constructor__)) colo_compare_init_globals(void) |
1481 | { | |
1482 | colo_compare_active = false; | |
1483 | qemu_mutex_init(&colo_compare_mutex); | |
1484 | } | |
1485 | ||
7dce4e6f ZC |
1486 | static const TypeInfo colo_compare_info = { |
1487 | .name = TYPE_COLO_COMPARE, | |
1488 | .parent = TYPE_OBJECT, | |
1489 | .instance_size = sizeof(CompareState), | |
1490 | .instance_init = colo_compare_init, | |
1491 | .instance_finalize = colo_compare_finalize, | |
1492 | .class_size = sizeof(CompareClass), | |
1493 | .class_init = colo_compare_class_init, | |
1494 | .interfaces = (InterfaceInfo[]) { | |
1495 | { TYPE_USER_CREATABLE }, | |
1496 | { } | |
1497 | } | |
1498 | }; | |
1499 | ||
1500 | static void register_types(void) | |
1501 | { | |
1502 | type_register_static(&colo_compare_info); | |
1503 | } | |
1504 | ||
1505 | type_init(register_types); |