]>
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 <[email protected]> | |
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 ZC |
34 | |
35 | #define TYPE_COLO_COMPARE "colo-compare" | |
36 | #define COLO_COMPARE(obj) \ | |
37 | OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE) | |
38 | ||
0ffcece3 ZC |
39 | static QTAILQ_HEAD(, CompareState) net_compares = |
40 | QTAILQ_HEAD_INITIALIZER(net_compares); | |
41 | ||
dccd0313 ZC |
42 | static NotifierList colo_compare_notifiers = |
43 | NOTIFIER_LIST_INITIALIZER(colo_compare_notifiers); | |
44 | ||
0682e15b | 45 | #define COMPARE_READ_LEN_MAX NET_BUFSIZE |
b6540d40 ZC |
46 | #define MAX_QUEUE_SIZE 1024 |
47 | ||
f449c9e5 MZ |
48 | #define COLO_COMPARE_FREE_PRIMARY 0x01 |
49 | #define COLO_COMPARE_FREE_SECONDARY 0x02 | |
50 | ||
0682e15b ZC |
51 | /* TODO: Should be configurable */ |
52 | #define REGULAR_PACKET_CHECK_MS 3000 | |
53 | ||
0ffcece3 ZC |
54 | static QemuMutex event_mtx; |
55 | static QemuCond event_complete_cond; | |
56 | static int event_unhandled_count; | |
57 | ||
59509ec1 | 58 | /* |
61c5f469 ZC |
59 | * + CompareState ++ |
60 | * | | | |
61 | * +---------------+ +---------------+ +---------------+ | |
62 | * | conn list + - > conn + ------- > conn + -- > ...... | |
63 | * +---------------+ +---------------+ +---------------+ | |
64 | * | | | | | | | |
65 | * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+ | |
66 | * |primary | |secondary |primary | |secondary | |
67 | * |packet | |packet + |packet | |packet + | |
68 | * +--------+ +--------+ +--------+ +--------+ | |
69 | * | | | | | |
70 | * +---v----+ +---v----+ +---v----+ +---v----+ | |
71 | * |primary | |secondary |primary | |secondary | |
72 | * |packet | |packet + |packet | |packet + | |
73 | * +--------+ +--------+ +--------+ +--------+ | |
74 | * | | | | | |
75 | * +---v----+ +---v----+ +---v----+ +---v----+ | |
76 | * |primary | |secondary |primary | |secondary | |
77 | * |packet | |packet + |packet | |packet + | |
78 | * +--------+ +--------+ +--------+ +--------+ | |
79 | */ | |
7dce4e6f ZC |
80 | typedef struct CompareState { |
81 | Object parent; | |
82 | ||
83 | char *pri_indev; | |
84 | char *sec_indev; | |
85 | char *outdev; | |
cf6af766 | 86 | char *notify_dev; |
32a6ebec MAL |
87 | CharBackend chr_pri_in; |
88 | CharBackend chr_sec_in; | |
89 | CharBackend chr_out; | |
13025fee | 90 | CharBackend chr_notify_dev; |
7dce4e6f ZC |
91 | SocketReadState pri_rs; |
92 | SocketReadState sec_rs; | |
13025fee | 93 | SocketReadState notify_rs; |
aa3a7032 | 94 | bool vnet_hdr; |
59509ec1 | 95 | |
61c5f469 ZC |
96 | /* |
97 | * Record the connection that through the NIC | |
98 | * Element type: Connection | |
b6540d40 ZC |
99 | */ |
100 | GQueue conn_list; | |
61c5f469 | 101 | /* Record the connection without repetition */ |
59509ec1 | 102 | GHashTable *connection_track_table; |
dfd917a9 | 103 | |
dd321ecf | 104 | IOThread *iothread; |
b43decb0 | 105 | GMainContext *worker_context; |
dd321ecf | 106 | QEMUTimer *packet_check_timer; |
0ffcece3 ZC |
107 | |
108 | QEMUBH *event_bh; | |
109 | enum colo_event event; | |
110 | ||
111 | QTAILQ_ENTRY(CompareState) next; | |
7dce4e6f ZC |
112 | } CompareState; |
113 | ||
114 | typedef struct CompareClass { | |
115 | ObjectClass parent_class; | |
116 | } CompareClass; | |
117 | ||
59509ec1 ZC |
118 | enum { |
119 | PRIMARY_IN = 0, | |
120 | SECONDARY_IN, | |
121 | }; | |
122 | ||
24525e93 | 123 | |
3037e7a5 | 124 | static int compare_chr_send(CompareState *s, |
59509ec1 | 125 | const uint8_t *buf, |
aa3a7032 | 126 | uint32_t size, |
30685c00 ZC |
127 | uint32_t vnet_hdr_len, |
128 | bool notify_remote_frame); | |
59509ec1 | 129 | |
f77bed14 ZC |
130 | static bool packet_matches_str(const char *str, |
131 | const uint8_t *buf, | |
132 | uint32_t packet_len) | |
133 | { | |
134 | if (packet_len != strlen(str)) { | |
135 | return false; | |
136 | } | |
137 | ||
138 | return !memcmp(str, buf, strlen(str)); | |
139 | } | |
140 | ||
1d09f700 ZC |
141 | static void notify_remote_frame(CompareState *s) |
142 | { | |
143 | char msg[] = "DO_CHECKPOINT"; | |
144 | int ret = 0; | |
145 | ||
146 | ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true); | |
147 | if (ret < 0) { | |
148 | error_report("Notify Xen COLO-frame failed"); | |
149 | } | |
150 | } | |
151 | ||
152 | static void colo_compare_inconsistency_notify(CompareState *s) | |
153 | { | |
154 | if (s->notify_dev) { | |
155 | notify_remote_frame(s); | |
156 | } else { | |
157 | notifier_list_notify(&colo_compare_notifiers, | |
158 | migrate_get_current()); | |
159 | } | |
160 | } | |
161 | ||
a935cc31 ZC |
162 | static gint seq_sorter(Packet *a, Packet *b, gpointer data) |
163 | { | |
e05ae1d9 | 164 | struct tcp_hdr *atcp, *btcp; |
a935cc31 | 165 | |
e05ae1d9 MAL |
166 | atcp = (struct tcp_hdr *)(a->transport_header); |
167 | btcp = (struct tcp_hdr *)(b->transport_header); | |
a935cc31 ZC |
168 | return ntohl(atcp->th_seq) - ntohl(btcp->th_seq); |
169 | } | |
170 | ||
f449c9e5 MZ |
171 | static void fill_pkt_tcp_info(void *data, uint32_t *max_ack) |
172 | { | |
173 | Packet *pkt = data; | |
e05ae1d9 | 174 | struct tcp_hdr *tcphd; |
f449c9e5 | 175 | |
e05ae1d9 | 176 | tcphd = (struct tcp_hdr *)pkt->transport_header; |
f449c9e5 MZ |
177 | |
178 | pkt->tcp_seq = ntohl(tcphd->th_seq); | |
179 | pkt->tcp_ack = ntohl(tcphd->th_ack); | |
180 | *max_ack = *max_ack > pkt->tcp_ack ? *max_ack : pkt->tcp_ack; | |
181 | pkt->header_size = pkt->transport_header - (uint8_t *)pkt->data | |
182 | + (tcphd->th_off << 2) - pkt->vnet_hdr_len; | |
183 | pkt->payload_size = pkt->size - pkt->header_size; | |
184 | pkt->seq_end = pkt->tcp_seq + pkt->payload_size; | |
185 | pkt->flags = tcphd->th_flags; | |
186 | } | |
187 | ||
8850d4ca MZ |
188 | /* |
189 | * Return 1 on success, if return 0 means the | |
190 | * packet will be dropped | |
191 | */ | |
f449c9e5 | 192 | static int colo_insert_packet(GQueue *queue, Packet *pkt, uint32_t *max_ack) |
8850d4ca MZ |
193 | { |
194 | if (g_queue_get_length(queue) <= MAX_QUEUE_SIZE) { | |
195 | if (pkt->ip->ip_p == IPPROTO_TCP) { | |
f449c9e5 | 196 | fill_pkt_tcp_info(pkt, max_ack); |
8850d4ca MZ |
197 | g_queue_insert_sorted(queue, |
198 | pkt, | |
199 | (GCompareDataFunc)seq_sorter, | |
200 | NULL); | |
201 | } else { | |
202 | g_queue_push_tail(queue, pkt); | |
203 | } | |
204 | return 1; | |
205 | } | |
206 | return 0; | |
207 | } | |
208 | ||
59509ec1 ZC |
209 | /* |
210 | * Return 0 on success, if return -1 means the pkt | |
211 | * is unsupported(arp and ipv6) and will be sent later | |
212 | */ | |
8ec14402 | 213 | static int packet_enqueue(CompareState *s, int mode, Connection **con) |
59509ec1 | 214 | { |
b6540d40 | 215 | ConnectionKey key; |
59509ec1 | 216 | Packet *pkt = NULL; |
b6540d40 | 217 | Connection *conn; |
59509ec1 ZC |
218 | |
219 | if (mode == PRIMARY_IN) { | |
ada1a33f ZC |
220 | pkt = packet_new(s->pri_rs.buf, |
221 | s->pri_rs.packet_len, | |
222 | s->pri_rs.vnet_hdr_len); | |
59509ec1 | 223 | } else { |
ada1a33f ZC |
224 | pkt = packet_new(s->sec_rs.buf, |
225 | s->sec_rs.packet_len, | |
226 | s->sec_rs.vnet_hdr_len); | |
59509ec1 ZC |
227 | } |
228 | ||
229 | if (parse_packet_early(pkt)) { | |
230 | packet_destroy(pkt, NULL); | |
231 | pkt = NULL; | |
232 | return -1; | |
233 | } | |
b6540d40 | 234 | fill_connection_key(pkt, &key); |
59509ec1 | 235 | |
b6540d40 ZC |
236 | conn = connection_get(s->connection_track_table, |
237 | &key, | |
238 | &s->conn_list); | |
59509ec1 | 239 | |
b6540d40 ZC |
240 | if (!conn->processing) { |
241 | g_queue_push_tail(&s->conn_list, conn); | |
242 | conn->processing = true; | |
243 | } | |
244 | ||
245 | if (mode == PRIMARY_IN) { | |
f449c9e5 | 246 | if (!colo_insert_packet(&conn->primary_list, pkt, &conn->pack)) { |
b6540d40 ZC |
247 | error_report("colo compare primary queue size too big," |
248 | "drop packet"); | |
249 | } | |
250 | } else { | |
f449c9e5 | 251 | if (!colo_insert_packet(&conn->secondary_list, pkt, &conn->sack)) { |
b6540d40 ZC |
252 | error_report("colo compare secondary queue size too big," |
253 | "drop packet"); | |
254 | } | |
255 | } | |
4d366235 | 256 | *con = conn; |
59509ec1 ZC |
257 | |
258 | return 0; | |
259 | } | |
260 | ||
f449c9e5 MZ |
261 | static inline bool after(uint32_t seq1, uint32_t seq2) |
262 | { | |
263 | return (int32_t)(seq1 - seq2) > 0; | |
264 | } | |
265 | ||
266 | static void colo_release_primary_pkt(CompareState *s, Packet *pkt) | |
267 | { | |
268 | int ret; | |
269 | ret = compare_chr_send(s, | |
270 | pkt->data, | |
271 | pkt->size, | |
30685c00 ZC |
272 | pkt->vnet_hdr_len, |
273 | false); | |
f449c9e5 MZ |
274 | if (ret < 0) { |
275 | error_report("colo send primary packet failed"); | |
276 | } | |
277 | trace_colo_compare_main("packet same and release packet"); | |
278 | packet_destroy(pkt, NULL); | |
279 | } | |
280 | ||
0682e15b ZC |
281 | /* |
282 | * The IP packets sent by primary and secondary | |
283 | * will be compared in here | |
284 | * TODO support ip fragment, Out-Of-Order | |
285 | * return: 0 means packet same | |
286 | * > 0 || < 0 means packet different | |
287 | */ | |
9394133f MZ |
288 | static int colo_compare_packet_payload(Packet *ppkt, |
289 | Packet *spkt, | |
290 | uint16_t poffset, | |
291 | uint16_t soffset, | |
292 | uint16_t len) | |
293 | ||
0682e15b | 294 | { |
d87aa138 | 295 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
e630b2bf ZC |
296 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; |
297 | ||
298 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
299 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
300 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
301 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
302 | ||
303 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
304 | pri_ip_dst, spkt->size, | |
305 | sec_ip_src, sec_ip_dst); | |
306 | } | |
0682e15b | 307 | |
9394133f | 308 | return memcmp(ppkt->data + poffset, spkt->data + soffset, len); |
0682e15b ZC |
309 | } |
310 | ||
f4b61836 | 311 | /* |
f449c9e5 MZ |
312 | * return true means that the payload is consist and |
313 | * need to make the next comparison, false means do | |
314 | * the checkpoint | |
315 | */ | |
316 | static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt, | |
317 | int8_t *mark, uint32_t max_ack) | |
0682e15b | 318 | { |
f449c9e5 MZ |
319 | *mark = 0; |
320 | ||
f449c9e5 | 321 | if (ppkt->tcp_seq == spkt->tcp_seq && ppkt->seq_end == spkt->seq_end) { |
1e907a32 | 322 | if (!colo_compare_packet_payload(ppkt, spkt, |
f449c9e5 MZ |
323 | ppkt->header_size, spkt->header_size, |
324 | ppkt->payload_size)) { | |
325 | *mark = COLO_COMPARE_FREE_SECONDARY | COLO_COMPARE_FREE_PRIMARY; | |
326 | return true; | |
327 | } | |
328 | } | |
f4b61836 | 329 | |
f449c9e5 MZ |
330 | /* one part of secondary packet payload still need to be compared */ |
331 | if (!after(ppkt->seq_end, spkt->seq_end)) { | |
1e907a32 | 332 | if (!colo_compare_packet_payload(ppkt, spkt, |
f449c9e5 MZ |
333 | ppkt->header_size + ppkt->offset, |
334 | spkt->header_size + spkt->offset, | |
335 | ppkt->payload_size - ppkt->offset)) { | |
336 | if (!after(ppkt->tcp_ack, max_ack)) { | |
337 | *mark = COLO_COMPARE_FREE_PRIMARY; | |
338 | spkt->offset += ppkt->payload_size - ppkt->offset; | |
339 | return true; | |
340 | } else { | |
341 | /* secondary guest hasn't ack the data, don't send | |
342 | * out this packet | |
343 | */ | |
344 | return false; | |
345 | } | |
346 | } | |
347 | } else { | |
348 | /* primary packet is longer than secondary packet, compare | |
349 | * the same part and mark the primary packet offset | |
350 | */ | |
1e907a32 | 351 | if (!colo_compare_packet_payload(ppkt, spkt, |
f449c9e5 MZ |
352 | ppkt->header_size + ppkt->offset, |
353 | spkt->header_size + spkt->offset, | |
354 | spkt->payload_size - spkt->offset)) { | |
355 | *mark = COLO_COMPARE_FREE_SECONDARY; | |
356 | ppkt->offset += spkt->payload_size - spkt->offset; | |
357 | return true; | |
358 | } | |
359 | } | |
360 | ||
361 | return false; | |
362 | } | |
2ad7ca4c | 363 | |
f449c9e5 MZ |
364 | static void colo_compare_tcp(CompareState *s, Connection *conn) |
365 | { | |
366 | Packet *ppkt = NULL, *spkt = NULL; | |
367 | int8_t mark; | |
f4b61836 ZC |
368 | |
369 | /* | |
f449c9e5 MZ |
370 | * If ppkt and spkt have the same payload, but ppkt's ACK |
371 | * is greater than spkt's ACK, in this case we can not | |
372 | * send the ppkt because it will cause the secondary guest | |
373 | * to miss sending some data in the next. Therefore, we | |
374 | * record the maximum ACK in the current queue at both | |
375 | * primary side and secondary side. Only when the ack is | |
376 | * less than the smaller of the two maximum ack, then we | |
377 | * can ensure that the packet's payload is acknowledged by | |
378 | * primary and secondary. | |
379 | */ | |
380 | uint32_t min_ack = conn->pack > conn->sack ? conn->sack : conn->pack; | |
381 | ||
382 | pri: | |
383 | if (g_queue_is_empty(&conn->primary_list)) { | |
384 | return; | |
385 | } | |
386 | ppkt = g_queue_pop_head(&conn->primary_list); | |
387 | sec: | |
388 | if (g_queue_is_empty(&conn->secondary_list)) { | |
389 | g_queue_push_head(&conn->primary_list, ppkt); | |
390 | return; | |
f4b61836 | 391 | } |
f449c9e5 | 392 | spkt = g_queue_pop_head(&conn->secondary_list); |
f4b61836 | 393 | |
f449c9e5 MZ |
394 | if (ppkt->tcp_seq == ppkt->seq_end) { |
395 | colo_release_primary_pkt(s, ppkt); | |
396 | ppkt = NULL; | |
397 | } | |
9394133f | 398 | |
f449c9e5 MZ |
399 | if (ppkt && conn->compare_seq && !after(ppkt->seq_end, conn->compare_seq)) { |
400 | trace_colo_compare_main("pri: this packet has compared"); | |
401 | colo_release_primary_pkt(s, ppkt); | |
402 | ppkt = NULL; | |
403 | } | |
9394133f | 404 | |
f449c9e5 MZ |
405 | if (spkt->tcp_seq == spkt->seq_end) { |
406 | packet_destroy(spkt, NULL); | |
407 | if (!ppkt) { | |
408 | goto pri; | |
409 | } else { | |
410 | goto sec; | |
411 | } | |
6efeb328 | 412 | } else { |
f449c9e5 MZ |
413 | if (conn->compare_seq && !after(spkt->seq_end, conn->compare_seq)) { |
414 | trace_colo_compare_main("sec: this packet has compared"); | |
415 | packet_destroy(spkt, NULL); | |
416 | if (!ppkt) { | |
417 | goto pri; | |
418 | } else { | |
419 | goto sec; | |
420 | } | |
421 | } | |
422 | if (!ppkt) { | |
423 | g_queue_push_head(&conn->secondary_list, spkt); | |
424 | goto pri; | |
425 | } | |
6efeb328 | 426 | } |
f4b61836 | 427 | |
f449c9e5 MZ |
428 | if (colo_mark_tcp_pkt(ppkt, spkt, &mark, min_ack)) { |
429 | trace_colo_compare_tcp_info("pri", | |
430 | ppkt->tcp_seq, ppkt->tcp_ack, | |
431 | ppkt->header_size, ppkt->payload_size, | |
432 | ppkt->offset, ppkt->flags); | |
433 | ||
434 | trace_colo_compare_tcp_info("sec", | |
435 | spkt->tcp_seq, spkt->tcp_ack, | |
436 | spkt->header_size, spkt->payload_size, | |
437 | spkt->offset, spkt->flags); | |
438 | ||
439 | if (mark == COLO_COMPARE_FREE_PRIMARY) { | |
440 | conn->compare_seq = ppkt->seq_end; | |
441 | colo_release_primary_pkt(s, ppkt); | |
442 | g_queue_push_head(&conn->secondary_list, spkt); | |
443 | goto pri; | |
444 | } | |
445 | if (mark == COLO_COMPARE_FREE_SECONDARY) { | |
446 | conn->compare_seq = spkt->seq_end; | |
447 | packet_destroy(spkt, NULL); | |
448 | goto sec; | |
449 | } | |
450 | if (mark == (COLO_COMPARE_FREE_PRIMARY | COLO_COMPARE_FREE_SECONDARY)) { | |
451 | conn->compare_seq = ppkt->seq_end; | |
452 | colo_release_primary_pkt(s, ppkt); | |
453 | packet_destroy(spkt, NULL); | |
454 | goto pri; | |
455 | } | |
456 | } else { | |
457 | g_queue_push_head(&conn->primary_list, ppkt); | |
458 | g_queue_push_head(&conn->secondary_list, spkt); | |
2061c14c ZC |
459 | |
460 | qemu_hexdump((char *)ppkt->data, stderr, | |
461 | "colo-compare ppkt", ppkt->size); | |
462 | qemu_hexdump((char *)spkt->data, stderr, | |
463 | "colo-compare spkt", spkt->size); | |
f4b61836 | 464 | |
1d09f700 | 465 | colo_compare_inconsistency_notify(s); |
f449c9e5 | 466 | } |
f4b61836 ZC |
467 | } |
468 | ||
f449c9e5 | 469 | |
f4b61836 ZC |
470 | /* |
471 | * Called from the compare thread on the primary | |
472 | * for compare udp packet | |
473 | */ | |
474 | static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt) | |
475 | { | |
9394133f MZ |
476 | uint16_t network_header_length = ppkt->ip->ip_hl << 2; |
477 | uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len; | |
f4b61836 ZC |
478 | |
479 | trace_colo_compare_main("compare udp"); | |
2ad7ca4c | 480 | |
6efeb328 ZC |
481 | /* |
482 | * Because of ppkt and spkt are both in the same connection, | |
483 | * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are | |
484 | * same with spkt. In addition, IP header's Identification is a random | |
485 | * field, we can handle it in IP fragmentation function later. | |
486 | * COLO just concern the response net packet payload from primary guest | |
487 | * and secondary guest are same or not, So we ignored all IP header include | |
488 | * other field like TOS,TTL,IP Checksum. we only need to compare | |
489 | * the ip payload here. | |
490 | */ | |
9394133f MZ |
491 | if (ppkt->size != spkt->size) { |
492 | trace_colo_compare_main("UDP: payload size of packets are different"); | |
493 | return -1; | |
494 | } | |
495 | if (colo_compare_packet_payload(ppkt, spkt, offset, offset, | |
496 | ppkt->size - offset)) { | |
f4b61836 | 497 | trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size); |
f4b61836 | 498 | trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size); |
d87aa138 | 499 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
1723a7f7 ZC |
500 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt", |
501 | ppkt->size); | |
502 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt", | |
503 | spkt->size); | |
504 | } | |
9394133f MZ |
505 | return -1; |
506 | } else { | |
507 | return 0; | |
f4b61836 | 508 | } |
f4b61836 ZC |
509 | } |
510 | ||
511 | /* | |
512 | * Called from the compare thread on the primary | |
513 | * for compare icmp packet | |
514 | */ | |
515 | static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt) | |
516 | { | |
9394133f MZ |
517 | uint16_t network_header_length = ppkt->ip->ip_hl << 2; |
518 | uint16_t offset = network_header_length + ETH_HLEN + ppkt->vnet_hdr_len; | |
6efeb328 | 519 | |
f4b61836 | 520 | trace_colo_compare_main("compare icmp"); |
f4b61836 | 521 | |
6efeb328 ZC |
522 | /* |
523 | * Because of ppkt and spkt are both in the same connection, | |
524 | * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are | |
525 | * same with spkt. In addition, IP header's Identification is a random | |
526 | * field, we can handle it in IP fragmentation function later. | |
527 | * COLO just concern the response net packet payload from primary guest | |
528 | * and secondary guest are same or not, So we ignored all IP header include | |
529 | * other field like TOS,TTL,IP Checksum. we only need to compare | |
530 | * the ip payload here. | |
531 | */ | |
9394133f MZ |
532 | if (ppkt->size != spkt->size) { |
533 | trace_colo_compare_main("ICMP: payload size of packets are different"); | |
534 | return -1; | |
535 | } | |
536 | if (colo_compare_packet_payload(ppkt, spkt, offset, offset, | |
537 | ppkt->size - offset)) { | |
f4b61836 ZC |
538 | trace_colo_compare_icmp_miscompare("primary pkt size", |
539 | ppkt->size); | |
f4b61836 ZC |
540 | trace_colo_compare_icmp_miscompare("Secondary pkt size", |
541 | spkt->size); | |
d87aa138 | 542 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
1723a7f7 ZC |
543 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt", |
544 | ppkt->size); | |
545 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt", | |
546 | spkt->size); | |
547 | } | |
f4b61836 ZC |
548 | return -1; |
549 | } else { | |
550 | return 0; | |
551 | } | |
552 | } | |
553 | ||
554 | /* | |
555 | * Called from the compare thread on the primary | |
556 | * for compare other packet | |
557 | */ | |
558 | static int colo_packet_compare_other(Packet *spkt, Packet *ppkt) | |
559 | { | |
9394133f MZ |
560 | uint16_t offset = ppkt->vnet_hdr_len; |
561 | ||
f4b61836 | 562 | trace_colo_compare_main("compare other"); |
d87aa138 | 563 | if (trace_event_get_state_backends(TRACE_COLO_COMPARE_MISCOMPARE)) { |
e630b2bf ZC |
564 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; |
565 | ||
566 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
567 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
568 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
569 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
570 | ||
571 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
572 | pri_ip_dst, spkt->size, | |
573 | sec_ip_src, sec_ip_dst); | |
574 | } | |
575 | ||
9394133f MZ |
576 | if (ppkt->size != spkt->size) { |
577 | trace_colo_compare_main("Other: payload size of packets are different"); | |
578 | return -1; | |
579 | } | |
580 | return colo_compare_packet_payload(ppkt, spkt, offset, offset, | |
581 | ppkt->size - offset); | |
0682e15b ZC |
582 | } |
583 | ||
584 | static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time) | |
585 | { | |
586 | int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST); | |
587 | ||
588 | if ((now - pkt->creation_ms) > (*check_time)) { | |
589 | trace_colo_old_packet_check_found(pkt->creation_ms); | |
590 | return 0; | |
591 | } else { | |
592 | return 1; | |
593 | } | |
594 | } | |
595 | ||
dccd0313 ZC |
596 | void colo_compare_register_notifier(Notifier *notify) |
597 | { | |
598 | notifier_list_add(&colo_compare_notifiers, notify); | |
599 | } | |
600 | ||
601 | void colo_compare_unregister_notifier(Notifier *notify) | |
602 | { | |
603 | notifier_remove(notify); | |
604 | } | |
605 | ||
d25a7dab | 606 | static int colo_old_packet_check_one_conn(Connection *conn, |
1d09f700 | 607 | CompareState *s) |
0682e15b | 608 | { |
0682e15b ZC |
609 | GList *result = NULL; |
610 | int64_t check_time = REGULAR_PACKET_CHECK_MS; | |
611 | ||
612 | result = g_queue_find_custom(&conn->primary_list, | |
613 | &check_time, | |
614 | (GCompareFunc)colo_old_packet_check_one); | |
615 | ||
616 | if (result) { | |
61c5f469 | 617 | /* Do checkpoint will flush old packet */ |
1d09f700 | 618 | colo_compare_inconsistency_notify(s); |
d25a7dab | 619 | return 0; |
0682e15b | 620 | } |
d25a7dab ZC |
621 | |
622 | return 1; | |
0682e15b ZC |
623 | } |
624 | ||
625 | /* | |
626 | * Look for old packets that the secondary hasn't matched, | |
627 | * if we have some then we have to checkpoint to wake | |
628 | * the secondary up. | |
629 | */ | |
630 | static void colo_old_packet_check(void *opaque) | |
631 | { | |
632 | CompareState *s = opaque; | |
633 | ||
d25a7dab ZC |
634 | /* |
635 | * If we find one old packet, stop finding job and notify | |
636 | * COLO frame do checkpoint. | |
637 | */ | |
1d09f700 | 638 | g_queue_find_custom(&s->conn_list, s, |
d25a7dab | 639 | (GCompareFunc)colo_old_packet_check_one_conn); |
0682e15b ZC |
640 | } |
641 | ||
f449c9e5 MZ |
642 | static void colo_compare_packet(CompareState *s, Connection *conn, |
643 | int (*HandlePacket)(Packet *spkt, | |
644 | Packet *ppkt)) | |
0682e15b | 645 | { |
0682e15b ZC |
646 | Packet *pkt = NULL; |
647 | GList *result = NULL; | |
0682e15b ZC |
648 | |
649 | while (!g_queue_is_empty(&conn->primary_list) && | |
650 | !g_queue_is_empty(&conn->secondary_list)) { | |
626bba98 | 651 | pkt = g_queue_pop_head(&conn->primary_list); |
f449c9e5 MZ |
652 | result = g_queue_find_custom(&conn->secondary_list, |
653 | pkt, (GCompareFunc)HandlePacket); | |
0682e15b ZC |
654 | |
655 | if (result) { | |
f449c9e5 | 656 | colo_release_primary_pkt(s, pkt); |
0682e15b | 657 | g_queue_remove(&conn->secondary_list, result->data); |
0682e15b ZC |
658 | } else { |
659 | /* | |
660 | * If one packet arrive late, the secondary_list or | |
661 | * primary_list will be empty, so we can't compare it | |
dccd0313 ZC |
662 | * until next comparison. If the packets in the list are |
663 | * timeout, it will trigger a checkpoint request. | |
0682e15b ZC |
664 | */ |
665 | trace_colo_compare_main("packet different"); | |
626bba98 | 666 | g_queue_push_head(&conn->primary_list, pkt); |
1d09f700 ZC |
667 | |
668 | colo_compare_inconsistency_notify(s); | |
0682e15b ZC |
669 | break; |
670 | } | |
671 | } | |
672 | } | |
673 | ||
f449c9e5 MZ |
674 | /* |
675 | * Called from the compare thread on the primary | |
676 | * for compare packet with secondary list of the | |
677 | * specified connection when a new packet was | |
678 | * queued to it. | |
679 | */ | |
680 | static void colo_compare_connection(void *opaque, void *user_data) | |
681 | { | |
682 | CompareState *s = user_data; | |
683 | Connection *conn = opaque; | |
684 | ||
685 | switch (conn->ip_proto) { | |
686 | case IPPROTO_TCP: | |
687 | colo_compare_tcp(s, conn); | |
688 | break; | |
689 | case IPPROTO_UDP: | |
690 | colo_compare_packet(s, conn, colo_packet_compare_udp); | |
691 | break; | |
692 | case IPPROTO_ICMP: | |
693 | colo_compare_packet(s, conn, colo_packet_compare_icmp); | |
694 | break; | |
695 | default: | |
696 | colo_compare_packet(s, conn, colo_packet_compare_other); | |
697 | break; | |
698 | } | |
699 | } | |
700 | ||
3037e7a5 | 701 | static int compare_chr_send(CompareState *s, |
59509ec1 | 702 | const uint8_t *buf, |
aa3a7032 | 703 | uint32_t size, |
30685c00 ZC |
704 | uint32_t vnet_hdr_len, |
705 | bool notify_remote_frame) | |
59509ec1 ZC |
706 | { |
707 | int ret = 0; | |
708 | uint32_t len = htonl(size); | |
709 | ||
710 | if (!size) { | |
711 | return 0; | |
712 | } | |
713 | ||
30685c00 ZC |
714 | if (notify_remote_frame) { |
715 | ret = qemu_chr_fe_write_all(&s->chr_notify_dev, | |
716 | (uint8_t *)&len, | |
717 | sizeof(len)); | |
718 | } else { | |
719 | ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len)); | |
720 | } | |
721 | ||
59509ec1 ZC |
722 | if (ret != sizeof(len)) { |
723 | goto err; | |
724 | } | |
725 | ||
aa3a7032 ZC |
726 | if (s->vnet_hdr) { |
727 | /* | |
728 | * We send vnet header len make other module(like filter-redirector) | |
729 | * know how to parse net packet correctly. | |
730 | */ | |
731 | len = htonl(vnet_hdr_len); | |
30685c00 ZC |
732 | |
733 | if (!notify_remote_frame) { | |
734 | ret = qemu_chr_fe_write_all(&s->chr_out, | |
735 | (uint8_t *)&len, | |
736 | sizeof(len)); | |
737 | } | |
738 | ||
aa3a7032 ZC |
739 | if (ret != sizeof(len)) { |
740 | goto err; | |
741 | } | |
742 | } | |
743 | ||
30685c00 ZC |
744 | if (notify_remote_frame) { |
745 | ret = qemu_chr_fe_write_all(&s->chr_notify_dev, | |
746 | (uint8_t *)buf, | |
747 | size); | |
748 | } else { | |
749 | ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size); | |
750 | } | |
751 | ||
59509ec1 ZC |
752 | if (ret != size) { |
753 | goto err; | |
754 | } | |
755 | ||
756 | return 0; | |
757 | ||
758 | err: | |
759 | return ret < 0 ? ret : -EIO; | |
760 | } | |
761 | ||
0682e15b ZC |
762 | static int compare_chr_can_read(void *opaque) |
763 | { | |
764 | return COMPARE_READ_LEN_MAX; | |
765 | } | |
766 | ||
767 | /* | |
768 | * Called from the main thread on the primary for packets | |
769 | * arriving over the socket from the primary. | |
770 | */ | |
771 | static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size) | |
772 | { | |
773 | CompareState *s = COLO_COMPARE(opaque); | |
774 | int ret; | |
775 | ||
776 | ret = net_fill_rstate(&s->pri_rs, buf, size); | |
777 | if (ret == -1) { | |
81517ba3 | 778 | qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, NULL, |
39ab61c6 | 779 | NULL, NULL, true); |
0682e15b ZC |
780 | error_report("colo-compare primary_in error"); |
781 | } | |
782 | } | |
783 | ||
784 | /* | |
785 | * Called from the main thread on the primary for packets | |
786 | * arriving over the socket from the secondary. | |
787 | */ | |
788 | static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size) | |
789 | { | |
790 | CompareState *s = COLO_COMPARE(opaque); | |
791 | int ret; | |
792 | ||
793 | ret = net_fill_rstate(&s->sec_rs, buf, size); | |
794 | if (ret == -1) { | |
81517ba3 | 795 | qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, NULL, |
39ab61c6 | 796 | NULL, NULL, true); |
0682e15b ZC |
797 | error_report("colo-compare secondary_in error"); |
798 | } | |
799 | } | |
800 | ||
13025fee ZC |
801 | static void compare_notify_chr(void *opaque, const uint8_t *buf, int size) |
802 | { | |
803 | CompareState *s = COLO_COMPARE(opaque); | |
804 | int ret; | |
805 | ||
806 | ret = net_fill_rstate(&s->notify_rs, buf, size); | |
807 | if (ret == -1) { | |
808 | qemu_chr_fe_set_handlers(&s->chr_notify_dev, NULL, NULL, NULL, NULL, | |
809 | NULL, NULL, true); | |
810 | error_report("colo-compare notify_dev error"); | |
811 | } | |
812 | } | |
813 | ||
66d2a242 HZ |
814 | /* |
815 | * Check old packet regularly so it can watch for any packets | |
816 | * that the secondary hasn't produced equivalents of. | |
817 | */ | |
dd321ecf | 818 | static void check_old_packet_regular(void *opaque) |
66d2a242 HZ |
819 | { |
820 | CompareState *s = opaque; | |
821 | ||
822 | /* if have old packet we will notify checkpoint */ | |
823 | colo_old_packet_check(s); | |
dd321ecf WY |
824 | timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + |
825 | REGULAR_PACKET_CHECK_MS); | |
826 | } | |
827 | ||
0ffcece3 ZC |
828 | /* Public API, Used for COLO frame to notify compare event */ |
829 | void colo_notify_compares_event(void *opaque, int event, Error **errp) | |
830 | { | |
831 | CompareState *s; | |
832 | ||
833 | qemu_mutex_lock(&event_mtx); | |
834 | QTAILQ_FOREACH(s, &net_compares, next) { | |
835 | s->event = event; | |
836 | qemu_bh_schedule(s->event_bh); | |
837 | event_unhandled_count++; | |
838 | } | |
839 | /* Wait all compare threads to finish handling this event */ | |
840 | while (event_unhandled_count > 0) { | |
841 | qemu_cond_wait(&event_complete_cond, &event_mtx); | |
842 | } | |
843 | ||
844 | qemu_mutex_unlock(&event_mtx); | |
845 | } | |
846 | ||
dd321ecf WY |
847 | static void colo_compare_timer_init(CompareState *s) |
848 | { | |
849 | AioContext *ctx = iothread_get_aio_context(s->iothread); | |
66d2a242 | 850 | |
dd321ecf WY |
851 | s->packet_check_timer = aio_timer_new(ctx, QEMU_CLOCK_VIRTUAL, |
852 | SCALE_MS, check_old_packet_regular, | |
853 | s); | |
854 | timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + | |
855 | REGULAR_PACKET_CHECK_MS); | |
66d2a242 HZ |
856 | } |
857 | ||
dd321ecf | 858 | static void colo_compare_timer_del(CompareState *s) |
0682e15b | 859 | { |
dd321ecf WY |
860 | if (s->packet_check_timer) { |
861 | timer_del(s->packet_check_timer); | |
862 | timer_free(s->packet_check_timer); | |
863 | s->packet_check_timer = NULL; | |
864 | } | |
865 | } | |
0682e15b | 866 | |
0ffcece3 ZC |
867 | static void colo_flush_packets(void *opaque, void *user_data); |
868 | ||
869 | static void colo_compare_handle_event(void *opaque) | |
870 | { | |
871 | CompareState *s = opaque; | |
872 | ||
873 | switch (s->event) { | |
874 | case COLO_EVENT_CHECKPOINT: | |
875 | g_queue_foreach(&s->conn_list, colo_flush_packets, s); | |
876 | break; | |
877 | case COLO_EVENT_FAILOVER: | |
878 | break; | |
879 | default: | |
880 | break; | |
881 | } | |
882 | ||
0ffcece3 | 883 | qemu_mutex_lock(&event_mtx); |
78e4f446 | 884 | assert(event_unhandled_count > 0); |
0ffcece3 ZC |
885 | event_unhandled_count--; |
886 | qemu_cond_broadcast(&event_complete_cond); | |
887 | qemu_mutex_unlock(&event_mtx); | |
888 | } | |
889 | ||
dd321ecf WY |
890 | static void colo_compare_iothread(CompareState *s) |
891 | { | |
892 | object_ref(OBJECT(s->iothread)); | |
893 | s->worker_context = iothread_get_g_main_context(s->iothread); | |
0682e15b | 894 | |
5345fdb4 | 895 | qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read, |
81517ba3 AN |
896 | compare_pri_chr_in, NULL, NULL, |
897 | s, s->worker_context, true); | |
5345fdb4 | 898 | qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read, |
81517ba3 AN |
899 | compare_sec_chr_in, NULL, NULL, |
900 | s, s->worker_context, true); | |
13025fee ZC |
901 | if (s->notify_dev) { |
902 | qemu_chr_fe_set_handlers(&s->chr_notify_dev, compare_chr_can_read, | |
903 | compare_notify_chr, NULL, NULL, | |
904 | s, s->worker_context, true); | |
905 | } | |
0682e15b | 906 | |
dd321ecf | 907 | colo_compare_timer_init(s); |
0ffcece3 | 908 | s->event_bh = qemu_bh_new(colo_compare_handle_event, s); |
0682e15b ZC |
909 | } |
910 | ||
7dce4e6f ZC |
911 | static char *compare_get_pri_indev(Object *obj, Error **errp) |
912 | { | |
913 | CompareState *s = COLO_COMPARE(obj); | |
914 | ||
915 | return g_strdup(s->pri_indev); | |
916 | } | |
917 | ||
918 | static void compare_set_pri_indev(Object *obj, const char *value, Error **errp) | |
919 | { | |
920 | CompareState *s = COLO_COMPARE(obj); | |
921 | ||
922 | g_free(s->pri_indev); | |
923 | s->pri_indev = g_strdup(value); | |
924 | } | |
925 | ||
926 | static char *compare_get_sec_indev(Object *obj, Error **errp) | |
927 | { | |
928 | CompareState *s = COLO_COMPARE(obj); | |
929 | ||
930 | return g_strdup(s->sec_indev); | |
931 | } | |
932 | ||
933 | static void compare_set_sec_indev(Object *obj, const char *value, Error **errp) | |
934 | { | |
935 | CompareState *s = COLO_COMPARE(obj); | |
936 | ||
937 | g_free(s->sec_indev); | |
938 | s->sec_indev = g_strdup(value); | |
939 | } | |
940 | ||
941 | static char *compare_get_outdev(Object *obj, Error **errp) | |
942 | { | |
943 | CompareState *s = COLO_COMPARE(obj); | |
944 | ||
945 | return g_strdup(s->outdev); | |
946 | } | |
947 | ||
948 | static void compare_set_outdev(Object *obj, const char *value, Error **errp) | |
949 | { | |
950 | CompareState *s = COLO_COMPARE(obj); | |
951 | ||
952 | g_free(s->outdev); | |
953 | s->outdev = g_strdup(value); | |
954 | } | |
955 | ||
aa3a7032 ZC |
956 | static bool compare_get_vnet_hdr(Object *obj, Error **errp) |
957 | { | |
958 | CompareState *s = COLO_COMPARE(obj); | |
959 | ||
960 | return s->vnet_hdr; | |
961 | } | |
962 | ||
963 | static void compare_set_vnet_hdr(Object *obj, | |
964 | bool value, | |
965 | Error **errp) | |
966 | { | |
967 | CompareState *s = COLO_COMPARE(obj); | |
968 | ||
969 | s->vnet_hdr = value; | |
970 | } | |
971 | ||
cf6af766 ZC |
972 | static char *compare_get_notify_dev(Object *obj, Error **errp) |
973 | { | |
974 | CompareState *s = COLO_COMPARE(obj); | |
975 | ||
976 | return g_strdup(s->notify_dev); | |
977 | } | |
978 | ||
979 | static void compare_set_notify_dev(Object *obj, const char *value, Error **errp) | |
980 | { | |
981 | CompareState *s = COLO_COMPARE(obj); | |
982 | ||
983 | g_free(s->notify_dev); | |
984 | s->notify_dev = g_strdup(value); | |
985 | } | |
986 | ||
7dce4e6f ZC |
987 | static void compare_pri_rs_finalize(SocketReadState *pri_rs) |
988 | { | |
59509ec1 | 989 | CompareState *s = container_of(pri_rs, CompareState, pri_rs); |
8ec14402 | 990 | Connection *conn = NULL; |
59509ec1 | 991 | |
8ec14402 | 992 | if (packet_enqueue(s, PRIMARY_IN, &conn)) { |
59509ec1 | 993 | trace_colo_compare_main("primary: unsupported packet in"); |
aa3a7032 ZC |
994 | compare_chr_send(s, |
995 | pri_rs->buf, | |
996 | pri_rs->packet_len, | |
30685c00 ZC |
997 | pri_rs->vnet_hdr_len, |
998 | false); | |
0682e15b | 999 | } else { |
3463218c | 1000 | /* compare packet in the specified connection */ |
8ec14402 | 1001 | colo_compare_connection(conn, s); |
59509ec1 | 1002 | } |
7dce4e6f ZC |
1003 | } |
1004 | ||
1005 | static void compare_sec_rs_finalize(SocketReadState *sec_rs) | |
1006 | { | |
59509ec1 | 1007 | CompareState *s = container_of(sec_rs, CompareState, sec_rs); |
8ec14402 | 1008 | Connection *conn = NULL; |
59509ec1 | 1009 | |
8ec14402 | 1010 | if (packet_enqueue(s, SECONDARY_IN, &conn)) { |
59509ec1 | 1011 | trace_colo_compare_main("secondary: unsupported packet in"); |
0682e15b | 1012 | } else { |
3463218c | 1013 | /* compare packet in the specified connection */ |
8ec14402 | 1014 | colo_compare_connection(conn, s); |
59509ec1 | 1015 | } |
7dce4e6f ZC |
1016 | } |
1017 | ||
13025fee ZC |
1018 | static void compare_notify_rs_finalize(SocketReadState *notify_rs) |
1019 | { | |
1d09f700 ZC |
1020 | CompareState *s = container_of(notify_rs, CompareState, notify_rs); |
1021 | ||
f77bed14 | 1022 | const char msg[] = "COLO_COMPARE_GET_XEN_INIT"; |
1d09f700 ZC |
1023 | int ret; |
1024 | ||
f77bed14 ZC |
1025 | if (packet_matches_str("COLO_USERSPACE_PROXY_INIT", |
1026 | notify_rs->buf, | |
1027 | notify_rs->packet_len)) { | |
1d09f700 ZC |
1028 | ret = compare_chr_send(s, (uint8_t *)msg, strlen(msg), 0, true); |
1029 | if (ret < 0) { | |
1030 | error_report("Notify Xen COLO-frame INIT failed"); | |
1031 | } | |
f77bed14 ZC |
1032 | } else if (packet_matches_str("COLO_CHECKPOINT", |
1033 | notify_rs->buf, | |
1034 | notify_rs->packet_len)) { | |
1d09f700 ZC |
1035 | /* colo-compare do checkpoint, flush pri packet and remove sec packet */ |
1036 | g_queue_foreach(&s->conn_list, colo_flush_packets, s); | |
f77bed14 ZC |
1037 | } else { |
1038 | error_report("COLO compare got unsupported instruction"); | |
1d09f700 | 1039 | } |
13025fee | 1040 | } |
7dce4e6f ZC |
1041 | |
1042 | /* | |
1043 | * Return 0 is success. | |
1044 | * Return 1 is failed. | |
1045 | */ | |
0ec7b3e7 | 1046 | static int find_and_check_chardev(Chardev **chr, |
7dce4e6f ZC |
1047 | char *chr_name, |
1048 | Error **errp) | |
1049 | { | |
7dce4e6f ZC |
1050 | *chr = qemu_chr_find(chr_name); |
1051 | if (*chr == NULL) { | |
1052 | error_setg(errp, "Device '%s' not found", | |
1053 | chr_name); | |
1054 | return 1; | |
1055 | } | |
1056 | ||
0a73336d DB |
1057 | if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) { |
1058 | error_setg(errp, "chardev \"%s\" is not reconnectable", | |
7dce4e6f ZC |
1059 | chr_name); |
1060 | return 1; | |
1061 | } | |
fbf3cc3a | 1062 | |
269d25cd MAL |
1063 | if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_GCONTEXT)) { |
1064 | error_setg(errp, "chardev \"%s\" cannot switch context", | |
1065 | chr_name); | |
1066 | return 1; | |
1067 | } | |
1068 | ||
7dce4e6f ZC |
1069 | return 0; |
1070 | } | |
1071 | ||
1072 | /* | |
1073 | * Called from the main thread on the primary | |
1074 | * to setup colo-compare. | |
1075 | */ | |
1076 | static void colo_compare_complete(UserCreatable *uc, Error **errp) | |
1077 | { | |
1078 | CompareState *s = COLO_COMPARE(uc); | |
0ec7b3e7 | 1079 | Chardev *chr; |
7dce4e6f | 1080 | |
dd321ecf | 1081 | if (!s->pri_indev || !s->sec_indev || !s->outdev || !s->iothread) { |
7dce4e6f | 1082 | error_setg(errp, "colo compare needs 'primary_in' ," |
dd321ecf | 1083 | "'secondary_in','outdev','iothread' property set"); |
7dce4e6f ZC |
1084 | return; |
1085 | } else if (!strcmp(s->pri_indev, s->outdev) || | |
1086 | !strcmp(s->sec_indev, s->outdev) || | |
1087 | !strcmp(s->pri_indev, s->sec_indev)) { | |
1088 | error_setg(errp, "'indev' and 'outdev' could not be same " | |
1089 | "for compare module"); | |
1090 | return; | |
1091 | } | |
1092 | ||
5345fdb4 MAL |
1093 | if (find_and_check_chardev(&chr, s->pri_indev, errp) || |
1094 | !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) { | |
7dce4e6f ZC |
1095 | return; |
1096 | } | |
1097 | ||
5345fdb4 MAL |
1098 | if (find_and_check_chardev(&chr, s->sec_indev, errp) || |
1099 | !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) { | |
7dce4e6f ZC |
1100 | return; |
1101 | } | |
1102 | ||
5345fdb4 MAL |
1103 | if (find_and_check_chardev(&chr, s->outdev, errp) || |
1104 | !qemu_chr_fe_init(&s->chr_out, chr, errp)) { | |
7dce4e6f ZC |
1105 | return; |
1106 | } | |
1107 | ||
aa3a7032 ZC |
1108 | net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr); |
1109 | net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr); | |
7dce4e6f | 1110 | |
13025fee ZC |
1111 | /* Try to enable remote notify chardev, currently just for Xen COLO */ |
1112 | if (s->notify_dev) { | |
1113 | if (find_and_check_chardev(&chr, s->notify_dev, errp) || | |
1114 | !qemu_chr_fe_init(&s->chr_notify_dev, chr, errp)) { | |
1115 | return; | |
1116 | } | |
1117 | ||
1118 | net_socket_rs_init(&s->notify_rs, compare_notify_rs_finalize, | |
1119 | s->vnet_hdr); | |
1120 | } | |
1121 | ||
0ffcece3 ZC |
1122 | QTAILQ_INSERT_TAIL(&net_compares, s, next); |
1123 | ||
b6540d40 ZC |
1124 | g_queue_init(&s->conn_list); |
1125 | ||
0ffcece3 ZC |
1126 | qemu_mutex_init(&event_mtx); |
1127 | qemu_cond_init(&event_complete_cond); | |
1128 | ||
b6540d40 ZC |
1129 | s->connection_track_table = g_hash_table_new_full(connection_key_hash, |
1130 | connection_key_equal, | |
1131 | g_free, | |
1132 | connection_destroy); | |
59509ec1 | 1133 | |
dd321ecf | 1134 | colo_compare_iothread(s); |
7dce4e6f ZC |
1135 | return; |
1136 | } | |
1137 | ||
dfd917a9 HZ |
1138 | static void colo_flush_packets(void *opaque, void *user_data) |
1139 | { | |
1140 | CompareState *s = user_data; | |
1141 | Connection *conn = opaque; | |
1142 | Packet *pkt = NULL; | |
1143 | ||
1144 | while (!g_queue_is_empty(&conn->primary_list)) { | |
1145 | pkt = g_queue_pop_head(&conn->primary_list); | |
aa3a7032 ZC |
1146 | compare_chr_send(s, |
1147 | pkt->data, | |
1148 | pkt->size, | |
30685c00 ZC |
1149 | pkt->vnet_hdr_len, |
1150 | false); | |
dfd917a9 HZ |
1151 | packet_destroy(pkt, NULL); |
1152 | } | |
1153 | while (!g_queue_is_empty(&conn->secondary_list)) { | |
1154 | pkt = g_queue_pop_head(&conn->secondary_list); | |
1155 | packet_destroy(pkt, NULL); | |
1156 | } | |
1157 | } | |
1158 | ||
7dce4e6f ZC |
1159 | static void colo_compare_class_init(ObjectClass *oc, void *data) |
1160 | { | |
1161 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); | |
1162 | ||
1163 | ucc->complete = colo_compare_complete; | |
1164 | } | |
1165 | ||
1166 | static void colo_compare_init(Object *obj) | |
1167 | { | |
aa3a7032 ZC |
1168 | CompareState *s = COLO_COMPARE(obj); |
1169 | ||
7dce4e6f ZC |
1170 | object_property_add_str(obj, "primary_in", |
1171 | compare_get_pri_indev, compare_set_pri_indev, | |
1172 | NULL); | |
1173 | object_property_add_str(obj, "secondary_in", | |
1174 | compare_get_sec_indev, compare_set_sec_indev, | |
1175 | NULL); | |
1176 | object_property_add_str(obj, "outdev", | |
1177 | compare_get_outdev, compare_set_outdev, | |
1178 | NULL); | |
dd321ecf WY |
1179 | object_property_add_link(obj, "iothread", TYPE_IOTHREAD, |
1180 | (Object **)&s->iothread, | |
1181 | object_property_allow_set_link, | |
265b578c | 1182 | OBJ_PROP_LINK_STRONG, NULL); |
cf6af766 ZC |
1183 | /* This parameter just for Xen COLO */ |
1184 | object_property_add_str(obj, "notify_dev", | |
1185 | compare_get_notify_dev, compare_set_notify_dev, | |
1186 | NULL); | |
aa3a7032 ZC |
1187 | |
1188 | s->vnet_hdr = false; | |
1189 | object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr, | |
1190 | compare_set_vnet_hdr, NULL); | |
7dce4e6f ZC |
1191 | } |
1192 | ||
1193 | static void colo_compare_finalize(Object *obj) | |
1194 | { | |
1195 | CompareState *s = COLO_COMPARE(obj); | |
0ffcece3 | 1196 | CompareState *tmp = NULL; |
7dce4e6f | 1197 | |
1ce2610c MAL |
1198 | qemu_chr_fe_deinit(&s->chr_pri_in, false); |
1199 | qemu_chr_fe_deinit(&s->chr_sec_in, false); | |
1200 | qemu_chr_fe_deinit(&s->chr_out, false); | |
13025fee ZC |
1201 | if (s->notify_dev) { |
1202 | qemu_chr_fe_deinit(&s->chr_notify_dev, false); | |
1203 | } | |
1204 | ||
dd321ecf WY |
1205 | if (s->iothread) { |
1206 | colo_compare_timer_del(s); | |
1207 | } | |
0ffcece3 ZC |
1208 | |
1209 | qemu_bh_delete(s->event_bh); | |
1210 | ||
1211 | QTAILQ_FOREACH(tmp, &net_compares, next) { | |
1212 | if (tmp == s) { | |
1213 | QTAILQ_REMOVE(&net_compares, s, next); | |
1214 | break; | |
1215 | } | |
1216 | } | |
1217 | ||
dfd917a9 HZ |
1218 | /* Release all unhandled packets after compare thead exited */ |
1219 | g_queue_foreach(&s->conn_list, colo_flush_packets, s); | |
1220 | ||
727c2d76 | 1221 | g_queue_clear(&s->conn_list); |
0682e15b | 1222 | |
dd321ecf WY |
1223 | if (s->connection_track_table) { |
1224 | g_hash_table_destroy(s->connection_track_table); | |
1225 | } | |
1226 | ||
1227 | if (s->iothread) { | |
1228 | object_unref(OBJECT(s->iothread)); | |
1229 | } | |
0ffcece3 ZC |
1230 | |
1231 | qemu_mutex_destroy(&event_mtx); | |
1232 | qemu_cond_destroy(&event_complete_cond); | |
1233 | ||
7dce4e6f ZC |
1234 | g_free(s->pri_indev); |
1235 | g_free(s->sec_indev); | |
1236 | g_free(s->outdev); | |
cf6af766 | 1237 | g_free(s->notify_dev); |
7dce4e6f ZC |
1238 | } |
1239 | ||
1240 | static const TypeInfo colo_compare_info = { | |
1241 | .name = TYPE_COLO_COMPARE, | |
1242 | .parent = TYPE_OBJECT, | |
1243 | .instance_size = sizeof(CompareState), | |
1244 | .instance_init = colo_compare_init, | |
1245 | .instance_finalize = colo_compare_finalize, | |
1246 | .class_size = sizeof(CompareClass), | |
1247 | .class_init = colo_compare_class_init, | |
1248 | .interfaces = (InterfaceInfo[]) { | |
1249 | { TYPE_USER_CREATABLE }, | |
1250 | { } | |
1251 | } | |
1252 | }; | |
1253 | ||
1254 | static void register_types(void) | |
1255 | { | |
1256 | type_register_static(&colo_compare_info); | |
1257 | } | |
1258 | ||
1259 | type_init(register_types); |