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