]>
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" | |
16 | #include "qemu/error-report.h" | |
59509ec1 | 17 | #include "trace.h" |
7dce4e6f ZC |
18 | #include "qemu-common.h" |
19 | #include "qapi/qmp/qerror.h" | |
20 | #include "qapi/error.h" | |
21 | #include "net/net.h" | |
f4b61836 | 22 | #include "net/eth.h" |
7dce4e6f ZC |
23 | #include "qom/object_interfaces.h" |
24 | #include "qemu/iov.h" | |
25 | #include "qom/object.h" | |
26 | #include "qemu/typedefs.h" | |
27 | #include "net/queue.h" | |
28 | #include "sysemu/char.h" | |
29 | #include "qemu/sockets.h" | |
30 | #include "qapi-visit.h" | |
59509ec1 | 31 | #include "net/colo.h" |
7dce4e6f ZC |
32 | |
33 | #define TYPE_COLO_COMPARE "colo-compare" | |
34 | #define COLO_COMPARE(obj) \ | |
35 | OBJECT_CHECK(CompareState, (obj), TYPE_COLO_COMPARE) | |
36 | ||
0682e15b | 37 | #define COMPARE_READ_LEN_MAX NET_BUFSIZE |
b6540d40 ZC |
38 | #define MAX_QUEUE_SIZE 1024 |
39 | ||
0682e15b ZC |
40 | /* TODO: Should be configurable */ |
41 | #define REGULAR_PACKET_CHECK_MS 3000 | |
42 | ||
59509ec1 ZC |
43 | /* |
44 | + CompareState ++ | |
45 | | | | |
46 | +---------------+ +---------------+ +---------------+ | |
47 | |conn list +--->conn +--------->conn | | |
48 | +---------------+ +---------------+ +---------------+ | |
49 | | | | | | | | |
50 | +---------------+ +---v----+ +---v----+ +---v----+ +---v----+ | |
51 | |primary | |secondary |primary | |secondary | |
52 | |packet | |packet + |packet | |packet + | |
53 | +--------+ +--------+ +--------+ +--------+ | |
54 | | | | | | |
55 | +---v----+ +---v----+ +---v----+ +---v----+ | |
56 | |primary | |secondary |primary | |secondary | |
57 | |packet | |packet + |packet | |packet + | |
58 | +--------+ +--------+ +--------+ +--------+ | |
59 | | | | | | |
60 | +---v----+ +---v----+ +---v----+ +---v----+ | |
61 | |primary | |secondary |primary | |secondary | |
62 | |packet | |packet + |packet | |packet + | |
63 | +--------+ +--------+ +--------+ +--------+ | |
64 | */ | |
7dce4e6f ZC |
65 | typedef struct CompareState { |
66 | Object parent; | |
67 | ||
68 | char *pri_indev; | |
69 | char *sec_indev; | |
70 | char *outdev; | |
32a6ebec MAL |
71 | CharBackend chr_pri_in; |
72 | CharBackend chr_sec_in; | |
73 | CharBackend chr_out; | |
7dce4e6f ZC |
74 | SocketReadState pri_rs; |
75 | SocketReadState sec_rs; | |
59509ec1 | 76 | |
b6540d40 ZC |
77 | /* connection list: the connections belonged to this NIC could be found |
78 | * in this list. | |
79 | * element type: Connection | |
80 | */ | |
81 | GQueue conn_list; | |
59509ec1 ZC |
82 | /* hashtable to save connection */ |
83 | GHashTable *connection_track_table; | |
0682e15b ZC |
84 | /* compare thread, a thread for each NIC */ |
85 | QemuThread thread; | |
7dce4e6f ZC |
86 | } CompareState; |
87 | ||
88 | typedef struct CompareClass { | |
89 | ObjectClass parent_class; | |
90 | } CompareClass; | |
91 | ||
59509ec1 ZC |
92 | enum { |
93 | PRIMARY_IN = 0, | |
94 | SECONDARY_IN, | |
95 | }; | |
96 | ||
5345fdb4 | 97 | static int compare_chr_send(CharBackend *out, |
59509ec1 ZC |
98 | const uint8_t *buf, |
99 | uint32_t size); | |
100 | ||
a935cc31 ZC |
101 | static gint seq_sorter(Packet *a, Packet *b, gpointer data) |
102 | { | |
103 | struct tcphdr *atcp, *btcp; | |
104 | ||
105 | atcp = (struct tcphdr *)(a->transport_header); | |
106 | btcp = (struct tcphdr *)(b->transport_header); | |
107 | return ntohl(atcp->th_seq) - ntohl(btcp->th_seq); | |
108 | } | |
109 | ||
59509ec1 ZC |
110 | /* |
111 | * Return 0 on success, if return -1 means the pkt | |
112 | * is unsupported(arp and ipv6) and will be sent later | |
113 | */ | |
114 | static int packet_enqueue(CompareState *s, int mode) | |
115 | { | |
b6540d40 | 116 | ConnectionKey key; |
59509ec1 | 117 | Packet *pkt = NULL; |
b6540d40 | 118 | Connection *conn; |
59509ec1 ZC |
119 | |
120 | if (mode == PRIMARY_IN) { | |
121 | pkt = packet_new(s->pri_rs.buf, s->pri_rs.packet_len); | |
122 | } else { | |
123 | pkt = packet_new(s->sec_rs.buf, s->sec_rs.packet_len); | |
124 | } | |
125 | ||
126 | if (parse_packet_early(pkt)) { | |
127 | packet_destroy(pkt, NULL); | |
128 | pkt = NULL; | |
129 | return -1; | |
130 | } | |
b6540d40 | 131 | fill_connection_key(pkt, &key); |
59509ec1 | 132 | |
b6540d40 ZC |
133 | conn = connection_get(s->connection_track_table, |
134 | &key, | |
135 | &s->conn_list); | |
59509ec1 | 136 | |
b6540d40 ZC |
137 | if (!conn->processing) { |
138 | g_queue_push_tail(&s->conn_list, conn); | |
139 | conn->processing = true; | |
140 | } | |
141 | ||
142 | if (mode == PRIMARY_IN) { | |
143 | if (g_queue_get_length(&conn->primary_list) <= | |
144 | MAX_QUEUE_SIZE) { | |
145 | g_queue_push_tail(&conn->primary_list, pkt); | |
a935cc31 ZC |
146 | if (conn->ip_proto == IPPROTO_TCP) { |
147 | g_queue_sort(&conn->primary_list, | |
148 | (GCompareDataFunc)seq_sorter, | |
149 | NULL); | |
150 | } | |
b6540d40 ZC |
151 | } else { |
152 | error_report("colo compare primary queue size too big," | |
153 | "drop packet"); | |
154 | } | |
155 | } else { | |
156 | if (g_queue_get_length(&conn->secondary_list) <= | |
157 | MAX_QUEUE_SIZE) { | |
158 | g_queue_push_tail(&conn->secondary_list, pkt); | |
a935cc31 ZC |
159 | if (conn->ip_proto == IPPROTO_TCP) { |
160 | g_queue_sort(&conn->secondary_list, | |
161 | (GCompareDataFunc)seq_sorter, | |
162 | NULL); | |
163 | } | |
b6540d40 ZC |
164 | } else { |
165 | error_report("colo compare secondary queue size too big," | |
166 | "drop packet"); | |
167 | } | |
168 | } | |
59509ec1 ZC |
169 | |
170 | return 0; | |
171 | } | |
172 | ||
0682e15b ZC |
173 | /* |
174 | * The IP packets sent by primary and secondary | |
175 | * will be compared in here | |
176 | * TODO support ip fragment, Out-Of-Order | |
177 | * return: 0 means packet same | |
178 | * > 0 || < 0 means packet different | |
179 | */ | |
180 | static int colo_packet_compare(Packet *ppkt, Packet *spkt) | |
181 | { | |
182 | trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src), | |
183 | inet_ntoa(ppkt->ip->ip_dst), spkt->size, | |
184 | inet_ntoa(spkt->ip->ip_src), | |
185 | inet_ntoa(spkt->ip->ip_dst)); | |
186 | ||
187 | if (ppkt->size == spkt->size) { | |
188 | return memcmp(ppkt->data, spkt->data, spkt->size); | |
189 | } else { | |
190 | return -1; | |
191 | } | |
192 | } | |
193 | ||
f4b61836 ZC |
194 | /* |
195 | * Called from the compare thread on the primary | |
196 | * for compare tcp packet | |
197 | * compare_tcp copied from Dr. David Alan Gilbert's branch | |
198 | */ | |
199 | static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt) | |
0682e15b | 200 | { |
f4b61836 ZC |
201 | struct tcphdr *ptcp, *stcp; |
202 | int res; | |
f4b61836 ZC |
203 | |
204 | trace_colo_compare_main("compare tcp"); | |
205 | if (ppkt->size != spkt->size) { | |
206 | if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) { | |
207 | trace_colo_compare_main("pkt size not same"); | |
208 | } | |
209 | return -1; | |
210 | } | |
211 | ||
212 | ptcp = (struct tcphdr *)ppkt->transport_header; | |
213 | stcp = (struct tcphdr *)spkt->transport_header; | |
214 | ||
215 | /* | |
216 | * The 'identification' field in the IP header is *very* random | |
217 | * it almost never matches. Fudge this by ignoring differences in | |
218 | * unfragmented packets; they'll normally sort themselves out if different | |
219 | * anyway, and it should recover at the TCP level. | |
220 | * An alternative would be to get both the primary and secondary to rewrite | |
221 | * somehow; but that would need some sync traffic to sync the state | |
222 | */ | |
223 | if (ntohs(ppkt->ip->ip_off) & IP_DF) { | |
224 | spkt->ip->ip_id = ppkt->ip->ip_id; | |
225 | /* and the sum will be different if the IDs were different */ | |
226 | spkt->ip->ip_sum = ppkt->ip->ip_sum; | |
227 | } | |
228 | ||
229 | res = memcmp(ppkt->data + ETH_HLEN, spkt->data + ETH_HLEN, | |
230 | (spkt->size - ETH_HLEN)); | |
231 | ||
232 | if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) { | |
2dfe5113 AB |
233 | trace_colo_compare_pkt_info_src(inet_ntoa(ppkt->ip->ip_src), |
234 | ntohl(stcp->th_seq), | |
235 | ntohl(stcp->th_ack), | |
236 | res, stcp->th_flags, | |
237 | spkt->size); | |
238 | ||
239 | trace_colo_compare_pkt_info_dst(inet_ntoa(ppkt->ip->ip_dst), | |
240 | ntohl(ptcp->th_seq), | |
241 | ntohl(ptcp->th_ack), | |
242 | res, ptcp->th_flags, | |
243 | ppkt->size); | |
2061c14c ZC |
244 | |
245 | qemu_hexdump((char *)ppkt->data, stderr, | |
246 | "colo-compare ppkt", ppkt->size); | |
247 | qemu_hexdump((char *)spkt->data, stderr, | |
248 | "colo-compare spkt", spkt->size); | |
f4b61836 ZC |
249 | } |
250 | ||
251 | return res; | |
252 | } | |
253 | ||
254 | /* | |
255 | * Called from the compare thread on the primary | |
256 | * for compare udp packet | |
257 | */ | |
258 | static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt) | |
259 | { | |
260 | int ret; | |
261 | ||
262 | trace_colo_compare_main("compare udp"); | |
263 | ret = colo_packet_compare(ppkt, spkt); | |
264 | ||
265 | if (ret) { | |
266 | trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size); | |
267 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", ppkt->size); | |
268 | trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size); | |
269 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare", spkt->size); | |
270 | } | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
275 | /* | |
276 | * Called from the compare thread on the primary | |
277 | * for compare icmp packet | |
278 | */ | |
279 | static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt) | |
280 | { | |
281 | int network_length; | |
282 | ||
283 | trace_colo_compare_main("compare icmp"); | |
284 | network_length = ppkt->ip->ip_hl * 4; | |
285 | if (ppkt->size != spkt->size || | |
286 | ppkt->size < network_length + ETH_HLEN) { | |
287 | return -1; | |
288 | } | |
289 | ||
290 | if (colo_packet_compare(ppkt, spkt)) { | |
291 | trace_colo_compare_icmp_miscompare("primary pkt size", | |
292 | ppkt->size); | |
293 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", | |
294 | ppkt->size); | |
295 | trace_colo_compare_icmp_miscompare("Secondary pkt size", | |
296 | spkt->size); | |
297 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare", | |
298 | spkt->size); | |
299 | return -1; | |
300 | } else { | |
301 | return 0; | |
302 | } | |
303 | } | |
304 | ||
305 | /* | |
306 | * Called from the compare thread on the primary | |
307 | * for compare other packet | |
308 | */ | |
309 | static int colo_packet_compare_other(Packet *spkt, Packet *ppkt) | |
310 | { | |
311 | trace_colo_compare_main("compare other"); | |
312 | trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src), | |
313 | inet_ntoa(ppkt->ip->ip_dst), spkt->size, | |
314 | inet_ntoa(spkt->ip->ip_src), | |
315 | inet_ntoa(spkt->ip->ip_dst)); | |
0682e15b ZC |
316 | return colo_packet_compare(ppkt, spkt); |
317 | } | |
318 | ||
319 | static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time) | |
320 | { | |
321 | int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST); | |
322 | ||
323 | if ((now - pkt->creation_ms) > (*check_time)) { | |
324 | trace_colo_old_packet_check_found(pkt->creation_ms); | |
325 | return 0; | |
326 | } else { | |
327 | return 1; | |
328 | } | |
329 | } | |
330 | ||
331 | static void colo_old_packet_check_one_conn(void *opaque, | |
332 | void *user_data) | |
333 | { | |
334 | Connection *conn = opaque; | |
335 | GList *result = NULL; | |
336 | int64_t check_time = REGULAR_PACKET_CHECK_MS; | |
337 | ||
338 | result = g_queue_find_custom(&conn->primary_list, | |
339 | &check_time, | |
340 | (GCompareFunc)colo_old_packet_check_one); | |
341 | ||
342 | if (result) { | |
343 | /* do checkpoint will flush old packet */ | |
344 | /* TODO: colo_notify_checkpoint();*/ | |
345 | } | |
346 | } | |
347 | ||
348 | /* | |
349 | * Look for old packets that the secondary hasn't matched, | |
350 | * if we have some then we have to checkpoint to wake | |
351 | * the secondary up. | |
352 | */ | |
353 | static void colo_old_packet_check(void *opaque) | |
354 | { | |
355 | CompareState *s = opaque; | |
356 | ||
357 | g_queue_foreach(&s->conn_list, colo_old_packet_check_one_conn, NULL); | |
358 | } | |
359 | ||
360 | /* | |
361 | * Called from the compare thread on the primary | |
362 | * for compare connection | |
363 | */ | |
364 | static void colo_compare_connection(void *opaque, void *user_data) | |
365 | { | |
366 | CompareState *s = user_data; | |
367 | Connection *conn = opaque; | |
368 | Packet *pkt = NULL; | |
369 | GList *result = NULL; | |
370 | int ret; | |
371 | ||
372 | while (!g_queue_is_empty(&conn->primary_list) && | |
373 | !g_queue_is_empty(&conn->secondary_list)) { | |
0682e15b | 374 | pkt = g_queue_pop_tail(&conn->primary_list); |
f4b61836 ZC |
375 | switch (conn->ip_proto) { |
376 | case IPPROTO_TCP: | |
377 | result = g_queue_find_custom(&conn->secondary_list, | |
378 | pkt, (GCompareFunc)colo_packet_compare_tcp); | |
379 | break; | |
380 | case IPPROTO_UDP: | |
381 | result = g_queue_find_custom(&conn->secondary_list, | |
382 | pkt, (GCompareFunc)colo_packet_compare_udp); | |
383 | break; | |
384 | case IPPROTO_ICMP: | |
385 | result = g_queue_find_custom(&conn->secondary_list, | |
386 | pkt, (GCompareFunc)colo_packet_compare_icmp); | |
387 | break; | |
388 | default: | |
389 | result = g_queue_find_custom(&conn->secondary_list, | |
390 | pkt, (GCompareFunc)colo_packet_compare_other); | |
391 | break; | |
392 | } | |
0682e15b ZC |
393 | |
394 | if (result) { | |
5345fdb4 | 395 | ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size); |
0682e15b ZC |
396 | if (ret < 0) { |
397 | error_report("colo_send_primary_packet failed"); | |
398 | } | |
399 | trace_colo_compare_main("packet same and release packet"); | |
400 | g_queue_remove(&conn->secondary_list, result->data); | |
401 | packet_destroy(pkt, NULL); | |
402 | } else { | |
403 | /* | |
404 | * If one packet arrive late, the secondary_list or | |
405 | * primary_list will be empty, so we can't compare it | |
406 | * until next comparison. | |
407 | */ | |
408 | trace_colo_compare_main("packet different"); | |
0682e15b | 409 | g_queue_push_tail(&conn->primary_list, pkt); |
0682e15b ZC |
410 | /* TODO: colo_notify_checkpoint();*/ |
411 | break; | |
412 | } | |
413 | } | |
414 | } | |
415 | ||
5345fdb4 | 416 | static int compare_chr_send(CharBackend *out, |
59509ec1 ZC |
417 | const uint8_t *buf, |
418 | uint32_t size) | |
419 | { | |
420 | int ret = 0; | |
421 | uint32_t len = htonl(size); | |
422 | ||
423 | if (!size) { | |
424 | return 0; | |
425 | } | |
426 | ||
427 | ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len)); | |
428 | if (ret != sizeof(len)) { | |
429 | goto err; | |
430 | } | |
431 | ||
432 | ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size); | |
433 | if (ret != size) { | |
434 | goto err; | |
435 | } | |
436 | ||
437 | return 0; | |
438 | ||
439 | err: | |
440 | return ret < 0 ? ret : -EIO; | |
441 | } | |
442 | ||
0682e15b ZC |
443 | static int compare_chr_can_read(void *opaque) |
444 | { | |
445 | return COMPARE_READ_LEN_MAX; | |
446 | } | |
447 | ||
448 | /* | |
449 | * Called from the main thread on the primary for packets | |
450 | * arriving over the socket from the primary. | |
451 | */ | |
452 | static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size) | |
453 | { | |
454 | CompareState *s = COLO_COMPARE(opaque); | |
455 | int ret; | |
456 | ||
457 | ret = net_fill_rstate(&s->pri_rs, buf, size); | |
458 | if (ret == -1) { | |
39ab61c6 MAL |
459 | qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, |
460 | NULL, NULL, true); | |
0682e15b ZC |
461 | error_report("colo-compare primary_in error"); |
462 | } | |
463 | } | |
464 | ||
465 | /* | |
466 | * Called from the main thread on the primary for packets | |
467 | * arriving over the socket from the secondary. | |
468 | */ | |
469 | static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size) | |
470 | { | |
471 | CompareState *s = COLO_COMPARE(opaque); | |
472 | int ret; | |
473 | ||
474 | ret = net_fill_rstate(&s->sec_rs, buf, size); | |
475 | if (ret == -1) { | |
39ab61c6 MAL |
476 | qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, |
477 | NULL, NULL, true); | |
0682e15b ZC |
478 | error_report("colo-compare secondary_in error"); |
479 | } | |
480 | } | |
481 | ||
66d2a242 HZ |
482 | /* |
483 | * Check old packet regularly so it can watch for any packets | |
484 | * that the secondary hasn't produced equivalents of. | |
485 | */ | |
486 | static gboolean check_old_packet_regular(void *opaque) | |
487 | { | |
488 | CompareState *s = opaque; | |
489 | ||
490 | /* if have old packet we will notify checkpoint */ | |
491 | colo_old_packet_check(s); | |
492 | ||
493 | return TRUE; | |
494 | } | |
495 | ||
0682e15b ZC |
496 | static void *colo_compare_thread(void *opaque) |
497 | { | |
498 | GMainContext *worker_context; | |
499 | GMainLoop *compare_loop; | |
500 | CompareState *s = opaque; | |
66d2a242 | 501 | GSource *timeout_source; |
0682e15b ZC |
502 | |
503 | worker_context = g_main_context_new(); | |
504 | ||
5345fdb4 | 505 | qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read, |
39ab61c6 | 506 | compare_pri_chr_in, NULL, s, worker_context, true); |
5345fdb4 | 507 | qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read, |
39ab61c6 | 508 | compare_sec_chr_in, NULL, s, worker_context, true); |
0682e15b ZC |
509 | |
510 | compare_loop = g_main_loop_new(worker_context, FALSE); | |
511 | ||
66d2a242 HZ |
512 | /* To kick any packets that the secondary doesn't match */ |
513 | timeout_source = g_timeout_source_new(REGULAR_PACKET_CHECK_MS); | |
514 | g_source_set_callback(timeout_source, | |
515 | (GSourceFunc)check_old_packet_regular, s, NULL); | |
516 | g_source_attach(timeout_source, worker_context); | |
517 | ||
0682e15b ZC |
518 | g_main_loop_run(compare_loop); |
519 | ||
66d2a242 | 520 | g_source_unref(timeout_source); |
0682e15b ZC |
521 | g_main_loop_unref(compare_loop); |
522 | g_main_context_unref(worker_context); | |
523 | return NULL; | |
524 | } | |
525 | ||
7dce4e6f ZC |
526 | static char *compare_get_pri_indev(Object *obj, Error **errp) |
527 | { | |
528 | CompareState *s = COLO_COMPARE(obj); | |
529 | ||
530 | return g_strdup(s->pri_indev); | |
531 | } | |
532 | ||
533 | static void compare_set_pri_indev(Object *obj, const char *value, Error **errp) | |
534 | { | |
535 | CompareState *s = COLO_COMPARE(obj); | |
536 | ||
537 | g_free(s->pri_indev); | |
538 | s->pri_indev = g_strdup(value); | |
539 | } | |
540 | ||
541 | static char *compare_get_sec_indev(Object *obj, Error **errp) | |
542 | { | |
543 | CompareState *s = COLO_COMPARE(obj); | |
544 | ||
545 | return g_strdup(s->sec_indev); | |
546 | } | |
547 | ||
548 | static void compare_set_sec_indev(Object *obj, const char *value, Error **errp) | |
549 | { | |
550 | CompareState *s = COLO_COMPARE(obj); | |
551 | ||
552 | g_free(s->sec_indev); | |
553 | s->sec_indev = g_strdup(value); | |
554 | } | |
555 | ||
556 | static char *compare_get_outdev(Object *obj, Error **errp) | |
557 | { | |
558 | CompareState *s = COLO_COMPARE(obj); | |
559 | ||
560 | return g_strdup(s->outdev); | |
561 | } | |
562 | ||
563 | static void compare_set_outdev(Object *obj, const char *value, Error **errp) | |
564 | { | |
565 | CompareState *s = COLO_COMPARE(obj); | |
566 | ||
567 | g_free(s->outdev); | |
568 | s->outdev = g_strdup(value); | |
569 | } | |
570 | ||
571 | static void compare_pri_rs_finalize(SocketReadState *pri_rs) | |
572 | { | |
59509ec1 ZC |
573 | CompareState *s = container_of(pri_rs, CompareState, pri_rs); |
574 | ||
575 | if (packet_enqueue(s, PRIMARY_IN)) { | |
576 | trace_colo_compare_main("primary: unsupported packet in"); | |
5345fdb4 | 577 | compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len); |
0682e15b ZC |
578 | } else { |
579 | /* compare connection */ | |
580 | g_queue_foreach(&s->conn_list, colo_compare_connection, s); | |
59509ec1 | 581 | } |
7dce4e6f ZC |
582 | } |
583 | ||
584 | static void compare_sec_rs_finalize(SocketReadState *sec_rs) | |
585 | { | |
59509ec1 ZC |
586 | CompareState *s = container_of(sec_rs, CompareState, sec_rs); |
587 | ||
588 | if (packet_enqueue(s, SECONDARY_IN)) { | |
589 | trace_colo_compare_main("secondary: unsupported packet in"); | |
0682e15b ZC |
590 | } else { |
591 | /* compare connection */ | |
592 | g_queue_foreach(&s->conn_list, colo_compare_connection, s); | |
59509ec1 | 593 | } |
7dce4e6f ZC |
594 | } |
595 | ||
7dce4e6f ZC |
596 | |
597 | /* | |
598 | * Return 0 is success. | |
599 | * Return 1 is failed. | |
600 | */ | |
0ec7b3e7 | 601 | static int find_and_check_chardev(Chardev **chr, |
7dce4e6f ZC |
602 | char *chr_name, |
603 | Error **errp) | |
604 | { | |
7dce4e6f ZC |
605 | *chr = qemu_chr_find(chr_name); |
606 | if (*chr == NULL) { | |
607 | error_setg(errp, "Device '%s' not found", | |
608 | chr_name); | |
609 | return 1; | |
610 | } | |
611 | ||
0a73336d DB |
612 | if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) { |
613 | error_setg(errp, "chardev \"%s\" is not reconnectable", | |
7dce4e6f ZC |
614 | chr_name); |
615 | return 1; | |
616 | } | |
fbf3cc3a | 617 | |
7dce4e6f ZC |
618 | return 0; |
619 | } | |
620 | ||
621 | /* | |
622 | * Called from the main thread on the primary | |
623 | * to setup colo-compare. | |
624 | */ | |
625 | static void colo_compare_complete(UserCreatable *uc, Error **errp) | |
626 | { | |
627 | CompareState *s = COLO_COMPARE(uc); | |
0ec7b3e7 | 628 | Chardev *chr; |
0682e15b ZC |
629 | char thread_name[64]; |
630 | static int compare_id; | |
7dce4e6f ZC |
631 | |
632 | if (!s->pri_indev || !s->sec_indev || !s->outdev) { | |
633 | error_setg(errp, "colo compare needs 'primary_in' ," | |
634 | "'secondary_in','outdev' property set"); | |
635 | return; | |
636 | } else if (!strcmp(s->pri_indev, s->outdev) || | |
637 | !strcmp(s->sec_indev, s->outdev) || | |
638 | !strcmp(s->pri_indev, s->sec_indev)) { | |
639 | error_setg(errp, "'indev' and 'outdev' could not be same " | |
640 | "for compare module"); | |
641 | return; | |
642 | } | |
643 | ||
5345fdb4 MAL |
644 | if (find_and_check_chardev(&chr, s->pri_indev, errp) || |
645 | !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) { | |
7dce4e6f ZC |
646 | return; |
647 | } | |
648 | ||
5345fdb4 MAL |
649 | if (find_and_check_chardev(&chr, s->sec_indev, errp) || |
650 | !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) { | |
7dce4e6f ZC |
651 | return; |
652 | } | |
653 | ||
5345fdb4 MAL |
654 | if (find_and_check_chardev(&chr, s->outdev, errp) || |
655 | !qemu_chr_fe_init(&s->chr_out, chr, errp)) { | |
7dce4e6f ZC |
656 | return; |
657 | } | |
658 | ||
7dce4e6f ZC |
659 | net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize); |
660 | net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize); | |
661 | ||
b6540d40 ZC |
662 | g_queue_init(&s->conn_list); |
663 | ||
664 | s->connection_track_table = g_hash_table_new_full(connection_key_hash, | |
665 | connection_key_equal, | |
666 | g_free, | |
667 | connection_destroy); | |
59509ec1 | 668 | |
0682e15b ZC |
669 | sprintf(thread_name, "colo-compare %d", compare_id); |
670 | qemu_thread_create(&s->thread, thread_name, | |
671 | colo_compare_thread, s, | |
672 | QEMU_THREAD_JOINABLE); | |
673 | compare_id++; | |
674 | ||
7dce4e6f ZC |
675 | return; |
676 | } | |
677 | ||
678 | static void colo_compare_class_init(ObjectClass *oc, void *data) | |
679 | { | |
680 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); | |
681 | ||
682 | ucc->complete = colo_compare_complete; | |
683 | } | |
684 | ||
685 | static void colo_compare_init(Object *obj) | |
686 | { | |
687 | object_property_add_str(obj, "primary_in", | |
688 | compare_get_pri_indev, compare_set_pri_indev, | |
689 | NULL); | |
690 | object_property_add_str(obj, "secondary_in", | |
691 | compare_get_sec_indev, compare_set_sec_indev, | |
692 | NULL); | |
693 | object_property_add_str(obj, "outdev", | |
694 | compare_get_outdev, compare_set_outdev, | |
695 | NULL); | |
696 | } | |
697 | ||
698 | static void colo_compare_finalize(Object *obj) | |
699 | { | |
700 | CompareState *s = COLO_COMPARE(obj); | |
701 | ||
c39860e6 MAL |
702 | qemu_chr_fe_deinit(&s->chr_pri_in); |
703 | qemu_chr_fe_deinit(&s->chr_sec_in); | |
704 | qemu_chr_fe_deinit(&s->chr_out); | |
7dce4e6f | 705 | |
b6540d40 ZC |
706 | g_queue_free(&s->conn_list); |
707 | ||
0682e15b ZC |
708 | if (qemu_thread_is_self(&s->thread)) { |
709 | /* compare connection */ | |
710 | g_queue_foreach(&s->conn_list, colo_compare_connection, s); | |
711 | qemu_thread_join(&s->thread); | |
712 | } | |
713 | ||
7dce4e6f ZC |
714 | g_free(s->pri_indev); |
715 | g_free(s->sec_indev); | |
716 | g_free(s->outdev); | |
717 | } | |
718 | ||
719 | static const TypeInfo colo_compare_info = { | |
720 | .name = TYPE_COLO_COMPARE, | |
721 | .parent = TYPE_OBJECT, | |
722 | .instance_size = sizeof(CompareState), | |
723 | .instance_init = colo_compare_init, | |
724 | .instance_finalize = colo_compare_finalize, | |
725 | .class_size = sizeof(CompareClass), | |
726 | .class_init = colo_compare_class_init, | |
727 | .interfaces = (InterfaceInfo[]) { | |
728 | { TYPE_USER_CREATABLE }, | |
729 | { } | |
730 | } | |
731 | }; | |
732 | ||
733 | static void register_types(void) | |
734 | { | |
735 | type_register_static(&colo_compare_info); | |
736 | } | |
737 | ||
738 | type_init(register_types); |