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