]>
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" | |
4d43a603 | 28 | #include "chardev/char-fe.h" |
7dce4e6f ZC |
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; | |
dfd917a9 | 86 | |
b43decb0 | 87 | GMainContext *worker_context; |
dfd917a9 | 88 | GMainLoop *compare_loop; |
7dce4e6f ZC |
89 | } CompareState; |
90 | ||
91 | typedef struct CompareClass { | |
92 | ObjectClass parent_class; | |
93 | } CompareClass; | |
94 | ||
59509ec1 ZC |
95 | enum { |
96 | PRIMARY_IN = 0, | |
97 | SECONDARY_IN, | |
98 | }; | |
99 | ||
5345fdb4 | 100 | static int compare_chr_send(CharBackend *out, |
59509ec1 ZC |
101 | const uint8_t *buf, |
102 | uint32_t size); | |
103 | ||
a935cc31 ZC |
104 | static gint seq_sorter(Packet *a, Packet *b, gpointer data) |
105 | { | |
106 | struct tcphdr *atcp, *btcp; | |
107 | ||
108 | atcp = (struct tcphdr *)(a->transport_header); | |
109 | btcp = (struct tcphdr *)(b->transport_header); | |
110 | return ntohl(atcp->th_seq) - ntohl(btcp->th_seq); | |
111 | } | |
112 | ||
59509ec1 ZC |
113 | /* |
114 | * Return 0 on success, if return -1 means the pkt | |
115 | * is unsupported(arp and ipv6) and will be sent later | |
116 | */ | |
117 | static int packet_enqueue(CompareState *s, int mode) | |
118 | { | |
b6540d40 | 119 | ConnectionKey key; |
59509ec1 | 120 | Packet *pkt = NULL; |
b6540d40 | 121 | Connection *conn; |
59509ec1 ZC |
122 | |
123 | if (mode == PRIMARY_IN) { | |
124 | pkt = packet_new(s->pri_rs.buf, s->pri_rs.packet_len); | |
125 | } else { | |
126 | pkt = packet_new(s->sec_rs.buf, s->sec_rs.packet_len); | |
127 | } | |
128 | ||
129 | if (parse_packet_early(pkt)) { | |
130 | packet_destroy(pkt, NULL); | |
131 | pkt = NULL; | |
132 | return -1; | |
133 | } | |
b6540d40 | 134 | fill_connection_key(pkt, &key); |
59509ec1 | 135 | |
b6540d40 ZC |
136 | conn = connection_get(s->connection_track_table, |
137 | &key, | |
138 | &s->conn_list); | |
59509ec1 | 139 | |
b6540d40 ZC |
140 | if (!conn->processing) { |
141 | g_queue_push_tail(&s->conn_list, conn); | |
142 | conn->processing = true; | |
143 | } | |
144 | ||
145 | if (mode == PRIMARY_IN) { | |
146 | if (g_queue_get_length(&conn->primary_list) <= | |
147 | MAX_QUEUE_SIZE) { | |
148 | g_queue_push_tail(&conn->primary_list, pkt); | |
a935cc31 ZC |
149 | if (conn->ip_proto == IPPROTO_TCP) { |
150 | g_queue_sort(&conn->primary_list, | |
151 | (GCompareDataFunc)seq_sorter, | |
152 | NULL); | |
153 | } | |
b6540d40 ZC |
154 | } else { |
155 | error_report("colo compare primary queue size too big," | |
156 | "drop packet"); | |
157 | } | |
158 | } else { | |
159 | if (g_queue_get_length(&conn->secondary_list) <= | |
160 | MAX_QUEUE_SIZE) { | |
161 | g_queue_push_tail(&conn->secondary_list, pkt); | |
a935cc31 ZC |
162 | if (conn->ip_proto == IPPROTO_TCP) { |
163 | g_queue_sort(&conn->secondary_list, | |
164 | (GCompareDataFunc)seq_sorter, | |
165 | NULL); | |
166 | } | |
b6540d40 ZC |
167 | } else { |
168 | error_report("colo compare secondary queue size too big," | |
169 | "drop packet"); | |
170 | } | |
171 | } | |
59509ec1 ZC |
172 | |
173 | return 0; | |
174 | } | |
175 | ||
0682e15b ZC |
176 | /* |
177 | * The IP packets sent by primary and secondary | |
178 | * will be compared in here | |
179 | * TODO support ip fragment, Out-Of-Order | |
180 | * return: 0 means packet same | |
181 | * > 0 || < 0 means packet different | |
182 | */ | |
6efeb328 | 183 | static int colo_packet_compare_common(Packet *ppkt, Packet *spkt, int offset) |
0682e15b | 184 | { |
e630b2bf ZC |
185 | if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) { |
186 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; | |
187 | ||
188 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
189 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
190 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
191 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
192 | ||
193 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
194 | pri_ip_dst, spkt->size, | |
195 | sec_ip_src, sec_ip_dst); | |
196 | } | |
0682e15b ZC |
197 | |
198 | if (ppkt->size == spkt->size) { | |
6efeb328 ZC |
199 | return memcmp(ppkt->data + offset, spkt->data + offset, |
200 | spkt->size - offset); | |
0682e15b | 201 | } else { |
2ad7ca4c | 202 | trace_colo_compare_main("Net packet size are not the same"); |
0682e15b ZC |
203 | return -1; |
204 | } | |
205 | } | |
206 | ||
f4b61836 ZC |
207 | /* |
208 | * Called from the compare thread on the primary | |
209 | * for compare tcp packet | |
210 | * compare_tcp copied from Dr. David Alan Gilbert's branch | |
211 | */ | |
212 | static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt) | |
0682e15b | 213 | { |
f4b61836 ZC |
214 | struct tcphdr *ptcp, *stcp; |
215 | int res; | |
f4b61836 ZC |
216 | |
217 | trace_colo_compare_main("compare tcp"); | |
2ad7ca4c | 218 | |
f4b61836 ZC |
219 | ptcp = (struct tcphdr *)ppkt->transport_header; |
220 | stcp = (struct tcphdr *)spkt->transport_header; | |
221 | ||
222 | /* | |
223 | * The 'identification' field in the IP header is *very* random | |
224 | * it almost never matches. Fudge this by ignoring differences in | |
225 | * unfragmented packets; they'll normally sort themselves out if different | |
226 | * anyway, and it should recover at the TCP level. | |
227 | * An alternative would be to get both the primary and secondary to rewrite | |
228 | * somehow; but that would need some sync traffic to sync the state | |
229 | */ | |
230 | if (ntohs(ppkt->ip->ip_off) & IP_DF) { | |
231 | spkt->ip->ip_id = ppkt->ip->ip_id; | |
232 | /* and the sum will be different if the IDs were different */ | |
233 | spkt->ip->ip_sum = ppkt->ip->ip_sum; | |
234 | } | |
235 | ||
184d4d42 ZC |
236 | /* |
237 | * Check tcp header length for tcp option field. | |
238 | * th_off > 5 means this tcp packet have options field. | |
239 | * The tcp options maybe always different. | |
240 | * for example: | |
241 | * From RFC 7323. | |
242 | * TCP Timestamps option (TSopt): | |
243 | * Kind: 8 | |
244 | * | |
245 | * Length: 10 bytes | |
246 | * | |
247 | * +-------+-------+---------------------+---------------------+ | |
248 | * |Kind=8 | 10 | TS Value (TSval) |TS Echo Reply (TSecr)| | |
249 | * +-------+-------+---------------------+---------------------+ | |
250 | * 1 1 4 4 | |
251 | * | |
252 | * In this case the primary guest's timestamp always different with | |
253 | * the secondary guest's timestamp. COLO just focus on payload, | |
254 | * so we just need skip this field. | |
255 | */ | |
256 | if (ptcp->th_off > 5) { | |
257 | ptrdiff_t tcp_offset; | |
258 | tcp_offset = ppkt->transport_header - (uint8_t *)ppkt->data | |
259 | + (ptcp->th_off * 4); | |
260 | res = colo_packet_compare_common(ppkt, spkt, tcp_offset); | |
261 | } else if (ptcp->th_sum == stcp->th_sum) { | |
6efeb328 ZC |
262 | res = colo_packet_compare_common(ppkt, spkt, ETH_HLEN); |
263 | } else { | |
264 | res = -1; | |
265 | } | |
f4b61836 | 266 | |
51b9d495 | 267 | if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) { |
f583dca9 ZC |
268 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; |
269 | ||
270 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
271 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
272 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
273 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
274 | ||
275 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
276 | pri_ip_dst, spkt->size, | |
277 | sec_ip_src, sec_ip_dst); | |
278 | ||
279 | trace_colo_compare_tcp_info("pri tcp packet", | |
280 | ntohl(ptcp->th_seq), | |
281 | ntohl(ptcp->th_ack), | |
282 | res, ptcp->th_flags, | |
283 | ppkt->size); | |
284 | ||
285 | trace_colo_compare_tcp_info("sec tcp packet", | |
286 | ntohl(stcp->th_seq), | |
287 | ntohl(stcp->th_ack), | |
288 | res, stcp->th_flags, | |
289 | spkt->size); | |
2061c14c ZC |
290 | |
291 | qemu_hexdump((char *)ppkt->data, stderr, | |
292 | "colo-compare ppkt", ppkt->size); | |
293 | qemu_hexdump((char *)spkt->data, stderr, | |
294 | "colo-compare spkt", spkt->size); | |
f4b61836 ZC |
295 | } |
296 | ||
297 | return res; | |
298 | } | |
299 | ||
300 | /* | |
301 | * Called from the compare thread on the primary | |
302 | * for compare udp packet | |
303 | */ | |
304 | static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt) | |
305 | { | |
306 | int ret; | |
6efeb328 | 307 | int network_header_length = ppkt->ip->ip_hl * 4; |
f4b61836 ZC |
308 | |
309 | trace_colo_compare_main("compare udp"); | |
2ad7ca4c | 310 | |
6efeb328 ZC |
311 | /* |
312 | * Because of ppkt and spkt are both in the same connection, | |
313 | * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are | |
314 | * same with spkt. In addition, IP header's Identification is a random | |
315 | * field, we can handle it in IP fragmentation function later. | |
316 | * COLO just concern the response net packet payload from primary guest | |
317 | * and secondary guest are same or not, So we ignored all IP header include | |
318 | * other field like TOS,TTL,IP Checksum. we only need to compare | |
319 | * the ip payload here. | |
320 | */ | |
321 | ret = colo_packet_compare_common(ppkt, spkt, | |
322 | network_header_length + ETH_HLEN); | |
f4b61836 ZC |
323 | |
324 | if (ret) { | |
325 | trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size); | |
f4b61836 | 326 | trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size); |
1723a7f7 ZC |
327 | if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) { |
328 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt", | |
329 | ppkt->size); | |
330 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt", | |
331 | spkt->size); | |
332 | } | |
f4b61836 ZC |
333 | } |
334 | ||
335 | return ret; | |
336 | } | |
337 | ||
338 | /* | |
339 | * Called from the compare thread on the primary | |
340 | * for compare icmp packet | |
341 | */ | |
342 | static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt) | |
343 | { | |
6efeb328 ZC |
344 | int network_header_length = ppkt->ip->ip_hl * 4; |
345 | ||
f4b61836 | 346 | trace_colo_compare_main("compare icmp"); |
f4b61836 | 347 | |
6efeb328 ZC |
348 | /* |
349 | * Because of ppkt and spkt are both in the same connection, | |
350 | * The ppkt's src ip, dst ip, src port, dst port, ip_proto all are | |
351 | * same with spkt. In addition, IP header's Identification is a random | |
352 | * field, we can handle it in IP fragmentation function later. | |
353 | * COLO just concern the response net packet payload from primary guest | |
354 | * and secondary guest are same or not, So we ignored all IP header include | |
355 | * other field like TOS,TTL,IP Checksum. we only need to compare | |
356 | * the ip payload here. | |
357 | */ | |
358 | if (colo_packet_compare_common(ppkt, spkt, | |
359 | network_header_length + ETH_HLEN)) { | |
f4b61836 ZC |
360 | trace_colo_compare_icmp_miscompare("primary pkt size", |
361 | ppkt->size); | |
f4b61836 ZC |
362 | trace_colo_compare_icmp_miscompare("Secondary pkt size", |
363 | spkt->size); | |
1723a7f7 ZC |
364 | if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) { |
365 | qemu_hexdump((char *)ppkt->data, stderr, "colo-compare pri pkt", | |
366 | ppkt->size); | |
367 | qemu_hexdump((char *)spkt->data, stderr, "colo-compare sec pkt", | |
368 | spkt->size); | |
369 | } | |
f4b61836 ZC |
370 | return -1; |
371 | } else { | |
372 | return 0; | |
373 | } | |
374 | } | |
375 | ||
376 | /* | |
377 | * Called from the compare thread on the primary | |
378 | * for compare other packet | |
379 | */ | |
380 | static int colo_packet_compare_other(Packet *spkt, Packet *ppkt) | |
381 | { | |
382 | trace_colo_compare_main("compare other"); | |
e630b2bf ZC |
383 | if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) { |
384 | char pri_ip_src[20], pri_ip_dst[20], sec_ip_src[20], sec_ip_dst[20]; | |
385 | ||
386 | strcpy(pri_ip_src, inet_ntoa(ppkt->ip->ip_src)); | |
387 | strcpy(pri_ip_dst, inet_ntoa(ppkt->ip->ip_dst)); | |
388 | strcpy(sec_ip_src, inet_ntoa(spkt->ip->ip_src)); | |
389 | strcpy(sec_ip_dst, inet_ntoa(spkt->ip->ip_dst)); | |
390 | ||
391 | trace_colo_compare_ip_info(ppkt->size, pri_ip_src, | |
392 | pri_ip_dst, spkt->size, | |
393 | sec_ip_src, sec_ip_dst); | |
394 | } | |
395 | ||
6efeb328 | 396 | return colo_packet_compare_common(ppkt, spkt, 0); |
0682e15b ZC |
397 | } |
398 | ||
399 | static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time) | |
400 | { | |
401 | int64_t now = qemu_clock_get_ms(QEMU_CLOCK_HOST); | |
402 | ||
403 | if ((now - pkt->creation_ms) > (*check_time)) { | |
404 | trace_colo_old_packet_check_found(pkt->creation_ms); | |
405 | return 0; | |
406 | } else { | |
407 | return 1; | |
408 | } | |
409 | } | |
410 | ||
d25a7dab ZC |
411 | static int colo_old_packet_check_one_conn(Connection *conn, |
412 | void *user_data) | |
0682e15b | 413 | { |
0682e15b ZC |
414 | GList *result = NULL; |
415 | int64_t check_time = REGULAR_PACKET_CHECK_MS; | |
416 | ||
417 | result = g_queue_find_custom(&conn->primary_list, | |
418 | &check_time, | |
419 | (GCompareFunc)colo_old_packet_check_one); | |
420 | ||
421 | if (result) { | |
422 | /* do checkpoint will flush old packet */ | |
423 | /* TODO: colo_notify_checkpoint();*/ | |
d25a7dab | 424 | return 0; |
0682e15b | 425 | } |
d25a7dab ZC |
426 | |
427 | return 1; | |
0682e15b ZC |
428 | } |
429 | ||
430 | /* | |
431 | * Look for old packets that the secondary hasn't matched, | |
432 | * if we have some then we have to checkpoint to wake | |
433 | * the secondary up. | |
434 | */ | |
435 | static void colo_old_packet_check(void *opaque) | |
436 | { | |
437 | CompareState *s = opaque; | |
438 | ||
d25a7dab ZC |
439 | /* |
440 | * If we find one old packet, stop finding job and notify | |
441 | * COLO frame do checkpoint. | |
442 | */ | |
443 | g_queue_find_custom(&s->conn_list, NULL, | |
444 | (GCompareFunc)colo_old_packet_check_one_conn); | |
0682e15b ZC |
445 | } |
446 | ||
447 | /* | |
448 | * Called from the compare thread on the primary | |
449 | * for compare connection | |
450 | */ | |
451 | static void colo_compare_connection(void *opaque, void *user_data) | |
452 | { | |
453 | CompareState *s = user_data; | |
454 | Connection *conn = opaque; | |
455 | Packet *pkt = NULL; | |
456 | GList *result = NULL; | |
457 | int ret; | |
458 | ||
459 | while (!g_queue_is_empty(&conn->primary_list) && | |
460 | !g_queue_is_empty(&conn->secondary_list)) { | |
0682e15b | 461 | pkt = g_queue_pop_tail(&conn->primary_list); |
f4b61836 ZC |
462 | switch (conn->ip_proto) { |
463 | case IPPROTO_TCP: | |
464 | result = g_queue_find_custom(&conn->secondary_list, | |
465 | pkt, (GCompareFunc)colo_packet_compare_tcp); | |
466 | break; | |
467 | case IPPROTO_UDP: | |
468 | result = g_queue_find_custom(&conn->secondary_list, | |
469 | pkt, (GCompareFunc)colo_packet_compare_udp); | |
470 | break; | |
471 | case IPPROTO_ICMP: | |
472 | result = g_queue_find_custom(&conn->secondary_list, | |
473 | pkt, (GCompareFunc)colo_packet_compare_icmp); | |
474 | break; | |
475 | default: | |
476 | result = g_queue_find_custom(&conn->secondary_list, | |
477 | pkt, (GCompareFunc)colo_packet_compare_other); | |
478 | break; | |
479 | } | |
0682e15b ZC |
480 | |
481 | if (result) { | |
5345fdb4 | 482 | ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size); |
0682e15b ZC |
483 | if (ret < 0) { |
484 | error_report("colo_send_primary_packet failed"); | |
485 | } | |
486 | trace_colo_compare_main("packet same and release packet"); | |
487 | g_queue_remove(&conn->secondary_list, result->data); | |
488 | packet_destroy(pkt, NULL); | |
489 | } else { | |
490 | /* | |
491 | * If one packet arrive late, the secondary_list or | |
492 | * primary_list will be empty, so we can't compare it | |
493 | * until next comparison. | |
494 | */ | |
495 | trace_colo_compare_main("packet different"); | |
0682e15b | 496 | g_queue_push_tail(&conn->primary_list, pkt); |
0682e15b ZC |
497 | /* TODO: colo_notify_checkpoint();*/ |
498 | break; | |
499 | } | |
500 | } | |
501 | } | |
502 | ||
5345fdb4 | 503 | static int compare_chr_send(CharBackend *out, |
59509ec1 ZC |
504 | const uint8_t *buf, |
505 | uint32_t size) | |
506 | { | |
507 | int ret = 0; | |
508 | uint32_t len = htonl(size); | |
509 | ||
510 | if (!size) { | |
511 | return 0; | |
512 | } | |
513 | ||
514 | ret = qemu_chr_fe_write_all(out, (uint8_t *)&len, sizeof(len)); | |
515 | if (ret != sizeof(len)) { | |
516 | goto err; | |
517 | } | |
518 | ||
519 | ret = qemu_chr_fe_write_all(out, (uint8_t *)buf, size); | |
520 | if (ret != size) { | |
521 | goto err; | |
522 | } | |
523 | ||
524 | return 0; | |
525 | ||
526 | err: | |
527 | return ret < 0 ? ret : -EIO; | |
528 | } | |
529 | ||
0682e15b ZC |
530 | static int compare_chr_can_read(void *opaque) |
531 | { | |
532 | return COMPARE_READ_LEN_MAX; | |
533 | } | |
534 | ||
535 | /* | |
536 | * Called from the main thread on the primary for packets | |
537 | * arriving over the socket from the primary. | |
538 | */ | |
539 | static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size) | |
540 | { | |
541 | CompareState *s = COLO_COMPARE(opaque); | |
542 | int ret; | |
543 | ||
544 | ret = net_fill_rstate(&s->pri_rs, buf, size); | |
545 | if (ret == -1) { | |
39ab61c6 MAL |
546 | qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL, |
547 | NULL, NULL, true); | |
0682e15b ZC |
548 | error_report("colo-compare primary_in error"); |
549 | } | |
550 | } | |
551 | ||
552 | /* | |
553 | * Called from the main thread on the primary for packets | |
554 | * arriving over the socket from the secondary. | |
555 | */ | |
556 | static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size) | |
557 | { | |
558 | CompareState *s = COLO_COMPARE(opaque); | |
559 | int ret; | |
560 | ||
561 | ret = net_fill_rstate(&s->sec_rs, buf, size); | |
562 | if (ret == -1) { | |
39ab61c6 MAL |
563 | qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL, |
564 | NULL, NULL, true); | |
0682e15b ZC |
565 | error_report("colo-compare secondary_in error"); |
566 | } | |
567 | } | |
568 | ||
66d2a242 HZ |
569 | /* |
570 | * Check old packet regularly so it can watch for any packets | |
571 | * that the secondary hasn't produced equivalents of. | |
572 | */ | |
573 | static gboolean check_old_packet_regular(void *opaque) | |
574 | { | |
575 | CompareState *s = opaque; | |
576 | ||
577 | /* if have old packet we will notify checkpoint */ | |
578 | colo_old_packet_check(s); | |
579 | ||
580 | return TRUE; | |
581 | } | |
582 | ||
0682e15b ZC |
583 | static void *colo_compare_thread(void *opaque) |
584 | { | |
0682e15b | 585 | CompareState *s = opaque; |
66d2a242 | 586 | GSource *timeout_source; |
0682e15b | 587 | |
b43decb0 | 588 | s->worker_context = g_main_context_new(); |
0682e15b | 589 | |
5345fdb4 | 590 | qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read, |
b43decb0 | 591 | compare_pri_chr_in, NULL, s, s->worker_context, true); |
5345fdb4 | 592 | qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read, |
b43decb0 | 593 | compare_sec_chr_in, NULL, s, s->worker_context, true); |
0682e15b | 594 | |
b43decb0 | 595 | s->compare_loop = g_main_loop_new(s->worker_context, FALSE); |
0682e15b | 596 | |
66d2a242 HZ |
597 | /* To kick any packets that the secondary doesn't match */ |
598 | timeout_source = g_timeout_source_new(REGULAR_PACKET_CHECK_MS); | |
599 | g_source_set_callback(timeout_source, | |
600 | (GSourceFunc)check_old_packet_regular, s, NULL); | |
b43decb0 | 601 | g_source_attach(timeout_source, s->worker_context); |
66d2a242 | 602 | |
dfd917a9 | 603 | g_main_loop_run(s->compare_loop); |
0682e15b | 604 | |
66d2a242 | 605 | g_source_unref(timeout_source); |
dfd917a9 | 606 | g_main_loop_unref(s->compare_loop); |
b43decb0 | 607 | g_main_context_unref(s->worker_context); |
0682e15b ZC |
608 | return NULL; |
609 | } | |
610 | ||
7dce4e6f ZC |
611 | static char *compare_get_pri_indev(Object *obj, Error **errp) |
612 | { | |
613 | CompareState *s = COLO_COMPARE(obj); | |
614 | ||
615 | return g_strdup(s->pri_indev); | |
616 | } | |
617 | ||
618 | static void compare_set_pri_indev(Object *obj, const char *value, Error **errp) | |
619 | { | |
620 | CompareState *s = COLO_COMPARE(obj); | |
621 | ||
622 | g_free(s->pri_indev); | |
623 | s->pri_indev = g_strdup(value); | |
624 | } | |
625 | ||
626 | static char *compare_get_sec_indev(Object *obj, Error **errp) | |
627 | { | |
628 | CompareState *s = COLO_COMPARE(obj); | |
629 | ||
630 | return g_strdup(s->sec_indev); | |
631 | } | |
632 | ||
633 | static void compare_set_sec_indev(Object *obj, const char *value, Error **errp) | |
634 | { | |
635 | CompareState *s = COLO_COMPARE(obj); | |
636 | ||
637 | g_free(s->sec_indev); | |
638 | s->sec_indev = g_strdup(value); | |
639 | } | |
640 | ||
641 | static char *compare_get_outdev(Object *obj, Error **errp) | |
642 | { | |
643 | CompareState *s = COLO_COMPARE(obj); | |
644 | ||
645 | return g_strdup(s->outdev); | |
646 | } | |
647 | ||
648 | static void compare_set_outdev(Object *obj, const char *value, Error **errp) | |
649 | { | |
650 | CompareState *s = COLO_COMPARE(obj); | |
651 | ||
652 | g_free(s->outdev); | |
653 | s->outdev = g_strdup(value); | |
654 | } | |
655 | ||
656 | static void compare_pri_rs_finalize(SocketReadState *pri_rs) | |
657 | { | |
59509ec1 ZC |
658 | CompareState *s = container_of(pri_rs, CompareState, pri_rs); |
659 | ||
660 | if (packet_enqueue(s, PRIMARY_IN)) { | |
661 | trace_colo_compare_main("primary: unsupported packet in"); | |
5345fdb4 | 662 | compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len); |
0682e15b ZC |
663 | } else { |
664 | /* compare connection */ | |
665 | g_queue_foreach(&s->conn_list, colo_compare_connection, s); | |
59509ec1 | 666 | } |
7dce4e6f ZC |
667 | } |
668 | ||
669 | static void compare_sec_rs_finalize(SocketReadState *sec_rs) | |
670 | { | |
59509ec1 ZC |
671 | CompareState *s = container_of(sec_rs, CompareState, sec_rs); |
672 | ||
673 | if (packet_enqueue(s, SECONDARY_IN)) { | |
674 | trace_colo_compare_main("secondary: unsupported packet in"); | |
0682e15b ZC |
675 | } else { |
676 | /* compare connection */ | |
677 | g_queue_foreach(&s->conn_list, colo_compare_connection, s); | |
59509ec1 | 678 | } |
7dce4e6f ZC |
679 | } |
680 | ||
7dce4e6f ZC |
681 | |
682 | /* | |
683 | * Return 0 is success. | |
684 | * Return 1 is failed. | |
685 | */ | |
0ec7b3e7 | 686 | static int find_and_check_chardev(Chardev **chr, |
7dce4e6f ZC |
687 | char *chr_name, |
688 | Error **errp) | |
689 | { | |
7dce4e6f ZC |
690 | *chr = qemu_chr_find(chr_name); |
691 | if (*chr == NULL) { | |
692 | error_setg(errp, "Device '%s' not found", | |
693 | chr_name); | |
694 | return 1; | |
695 | } | |
696 | ||
0a73336d DB |
697 | if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) { |
698 | error_setg(errp, "chardev \"%s\" is not reconnectable", | |
7dce4e6f ZC |
699 | chr_name); |
700 | return 1; | |
701 | } | |
fbf3cc3a | 702 | |
7dce4e6f ZC |
703 | return 0; |
704 | } | |
705 | ||
706 | /* | |
707 | * Called from the main thread on the primary | |
708 | * to setup colo-compare. | |
709 | */ | |
710 | static void colo_compare_complete(UserCreatable *uc, Error **errp) | |
711 | { | |
712 | CompareState *s = COLO_COMPARE(uc); | |
0ec7b3e7 | 713 | Chardev *chr; |
0682e15b ZC |
714 | char thread_name[64]; |
715 | static int compare_id; | |
7dce4e6f ZC |
716 | |
717 | if (!s->pri_indev || !s->sec_indev || !s->outdev) { | |
718 | error_setg(errp, "colo compare needs 'primary_in' ," | |
719 | "'secondary_in','outdev' property set"); | |
720 | return; | |
721 | } else if (!strcmp(s->pri_indev, s->outdev) || | |
722 | !strcmp(s->sec_indev, s->outdev) || | |
723 | !strcmp(s->pri_indev, s->sec_indev)) { | |
724 | error_setg(errp, "'indev' and 'outdev' could not be same " | |
725 | "for compare module"); | |
726 | return; | |
727 | } | |
728 | ||
5345fdb4 MAL |
729 | if (find_and_check_chardev(&chr, s->pri_indev, errp) || |
730 | !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) { | |
7dce4e6f ZC |
731 | return; |
732 | } | |
733 | ||
5345fdb4 MAL |
734 | if (find_and_check_chardev(&chr, s->sec_indev, errp) || |
735 | !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) { | |
7dce4e6f ZC |
736 | return; |
737 | } | |
738 | ||
5345fdb4 MAL |
739 | if (find_and_check_chardev(&chr, s->outdev, errp) || |
740 | !qemu_chr_fe_init(&s->chr_out, chr, errp)) { | |
7dce4e6f ZC |
741 | return; |
742 | } | |
743 | ||
7dce4e6f ZC |
744 | net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize); |
745 | net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize); | |
746 | ||
b6540d40 ZC |
747 | g_queue_init(&s->conn_list); |
748 | ||
749 | s->connection_track_table = g_hash_table_new_full(connection_key_hash, | |
750 | connection_key_equal, | |
751 | g_free, | |
752 | connection_destroy); | |
59509ec1 | 753 | |
0682e15b ZC |
754 | sprintf(thread_name, "colo-compare %d", compare_id); |
755 | qemu_thread_create(&s->thread, thread_name, | |
756 | colo_compare_thread, s, | |
757 | QEMU_THREAD_JOINABLE); | |
758 | compare_id++; | |
759 | ||
7dce4e6f ZC |
760 | return; |
761 | } | |
762 | ||
dfd917a9 HZ |
763 | static void colo_flush_packets(void *opaque, void *user_data) |
764 | { | |
765 | CompareState *s = user_data; | |
766 | Connection *conn = opaque; | |
767 | Packet *pkt = NULL; | |
768 | ||
769 | while (!g_queue_is_empty(&conn->primary_list)) { | |
770 | pkt = g_queue_pop_head(&conn->primary_list); | |
771 | compare_chr_send(&s->chr_out, pkt->data, pkt->size); | |
772 | packet_destroy(pkt, NULL); | |
773 | } | |
774 | while (!g_queue_is_empty(&conn->secondary_list)) { | |
775 | pkt = g_queue_pop_head(&conn->secondary_list); | |
776 | packet_destroy(pkt, NULL); | |
777 | } | |
778 | } | |
779 | ||
7dce4e6f ZC |
780 | static void colo_compare_class_init(ObjectClass *oc, void *data) |
781 | { | |
782 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); | |
783 | ||
784 | ucc->complete = colo_compare_complete; | |
785 | } | |
786 | ||
787 | static void colo_compare_init(Object *obj) | |
788 | { | |
789 | object_property_add_str(obj, "primary_in", | |
790 | compare_get_pri_indev, compare_set_pri_indev, | |
791 | NULL); | |
792 | object_property_add_str(obj, "secondary_in", | |
793 | compare_get_sec_indev, compare_set_sec_indev, | |
794 | NULL); | |
795 | object_property_add_str(obj, "outdev", | |
796 | compare_get_outdev, compare_set_outdev, | |
797 | NULL); | |
798 | } | |
799 | ||
800 | static void colo_compare_finalize(Object *obj) | |
801 | { | |
802 | CompareState *s = COLO_COMPARE(obj); | |
803 | ||
1ce2610c MAL |
804 | qemu_chr_fe_deinit(&s->chr_pri_in, false); |
805 | qemu_chr_fe_deinit(&s->chr_sec_in, false); | |
806 | qemu_chr_fe_deinit(&s->chr_out, false); | |
7dce4e6f | 807 | |
dfd917a9 HZ |
808 | g_main_loop_quit(s->compare_loop); |
809 | qemu_thread_join(&s->thread); | |
b6540d40 | 810 | |
dfd917a9 HZ |
811 | /* Release all unhandled packets after compare thead exited */ |
812 | g_queue_foreach(&s->conn_list, colo_flush_packets, s); | |
813 | ||
727c2d76 | 814 | g_queue_clear(&s->conn_list); |
0682e15b | 815 | |
dfd917a9 | 816 | g_hash_table_destroy(s->connection_track_table); |
7dce4e6f ZC |
817 | g_free(s->pri_indev); |
818 | g_free(s->sec_indev); | |
819 | g_free(s->outdev); | |
820 | } | |
821 | ||
822 | static const TypeInfo colo_compare_info = { | |
823 | .name = TYPE_COLO_COMPARE, | |
824 | .parent = TYPE_OBJECT, | |
825 | .instance_size = sizeof(CompareState), | |
826 | .instance_init = colo_compare_init, | |
827 | .instance_finalize = colo_compare_finalize, | |
828 | .class_size = sizeof(CompareClass), | |
829 | .class_init = colo_compare_class_init, | |
830 | .interfaces = (InterfaceInfo[]) { | |
831 | { TYPE_USER_CREATABLE }, | |
832 | { } | |
833 | } | |
834 | }; | |
835 | ||
836 | static void register_types(void) | |
837 | { | |
838 | type_register_static(&colo_compare_info); | |
839 | } | |
840 | ||
841 | type_init(register_types); |