Skip to contentSkip to conversation

Introduce COLO-compare

RFC PATCH Superseded Zhang Chen 2 participants 11 messages Jul 22, 2016 Networking

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Zhang Chen

author
#0 lore raw

v9 cover Introduce COLO-compare

COLO-compare is a part of COLO project. It is used to compare the network package to help COLO decide whether to do checkpoint.

The full version in this github:
https://github.com/zhangckid/qemu/tree/colo-v2.7-proxy-mode-compare-with-colo-base-jul22

v9:
p5:
- use chr_update_read_handler_full() replace
the chr_update_read_handler()
- use io_watch_poll_prepare_full() replace
the io_watch_poll_prepare()
- use io_watch_poll_funcs_full replace
the io_watch_poll_funcs
- avoid code duplication

v8:
p5:
- add new patch:
qemu-char: Add qemu_chr_add_handlers_full() for GMaincontext

v7:
p5:
- add [PATCH]qemu-char: Fix context for g_source_attach() in this patch series.

v6:
p6:
- add more commit log.
- fix icmp comparison to compare all packet.

p5:
- add more cpmments in commit log.
- change REGULAR_CHECK_MS to REGULAR_PACKET_CHECK_MS
- make check old packet independent to compare thread
- remove thread_status

p4:
- change this patch only about
Connection and ConnectionKey.
- add some comments in commit log.
- remove mode in fill_connection_key().
- fix some comments and bug.
- move colo_conn_state to patch of
"work with colo-frame"
- remove conn_list_lock.
- add MAX_QUEUE_SIZE, if primary_list or
secondary_list biger than MAX_QUEUE_SIZE
we will drop packet.

p3:
- add new independent kernel jhash patch.

p2:
- add new independent colo-base patch.

p1:
- add a ascii figure and some comments to explain it
- move trace.h to p2
- move QTAILQ_HEAD(, CompareState) net_compares to patch of "work with colo-frame"
- add some comments in qemu-option.hx

v5:
p3:
- comments from Jason
we poll and handle chardev in comapre thread, Through this way, there's no need for extra synchronization with main loop
this depend on another patch:
qemu-char: Fix context for g_source_attach()
- remove QemuEvent
p2:
- remove conn->list_lock
p1:
- move compare_pri/sec_chr_in to p3
- move compare_chr_send to p2

v4:
p4:
- add some comments
- fix some trace-events
- fix tcp compare error
p3:
- add rcu_read_lock().
- fix trace name
- fix jason's other comments
- rebase some Dave's branch function
p2:
- colo_compare_connection() change g_queue_push_head() to
- g_queue_push_tail() match to sorted order.
- remove pkt->s
- move data structure to colo-base.h
- add colo-base.c reuse codes for filter-rewriter
- add some filter-rewriter needs struct
- depends on previous SocketReadState patch
p1:
- except move qemu_chr_add_handlers()
to colo thread
- remove class_finalize
- remove secondary arp codes
- depends on previous SocketReadState patch

v3:
- rebase colo-compare to colo-frame v2.7
- fix most of Dave's comments
(except RCU)
- add TCP,UDP,ICMP and other packet comparison
- add trace-event
- add some comments
- other bug fix
- add RFC index
- add usage in patch 1/4

v2:
- add jhash.h

v1:
- initial patch

Zhang Chen (7):
colo-compare: introduce colo compare initialization
colo-base: add colo-base to define and handle packet
Jhash: add linux kernel jhashtable in qemu
colo-compare: track connection and enqueue packet
qemu-char: Add qemu_chr_add_handlers_full() for GMaincontext
colo-compare: introduce packet comparison thread
colo-compare: add TCP,UDP,ICMP packet comparison

 include/qemu/jhash.h  |  61 ++++
 include/sysemu/char.h |  11 +-
 net/Makefile.objs     |   2 +
 net/colo-base.c       | 183 ++++++++++++
 net/colo-base.h       |  71 +++++
 net/colo-compare.c    | 765 ++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-char.c           | 119 +++++---
 qemu-options.hx       |  38 +++
 trace-events          |   9 +
 vl.c                  |   3 +-
 10 files changed, 1215 insertions(+), 47 deletions(-)
 create mode 100644 include/qemu/jhash.h
 create mode 100644 net/colo-base.c
 create mode 100644 net/colo-base.h
 create mode 100644 net/colo-compare.c
Signature · 1 line
2.7.4
author
#1 lore raw

RFC PATCH v9 1/7 colo-compare: introduce colo compare initialization

This a COLO net ascii figure:

Primary qemu Secondary qemu

+----------------------------------------------------------+           +----------------------------------------------------------------+
| +-----------------------------------------------------+  |           |  +-----------------------------------------------------------+ |
| |                                                     |  |           |  |                                                           | |
| |                        guest                        |  |           |  |                        guest                              | |
| |                                                     |  |           |  |                                                           | |
| +-------^------------------+--------------------------+  |           |  +---------------------+--------+----------------------------+ |
|         |                  |                             |           |                        ^        |                              |
|         |                  |                             |           |                        |        |                              |
|         |  +------------------------------------------------------+  |                        |        |                              |
|netfilter|  |               |                             |        |  |   netfilter            |        |                              |
| +----------+ -----------------------+                    |        |  |  +-----------------------------------------------------------+ |
| |       |  |               |        |                    |        |  |  |                     |        |  filter excute order       | |
| |       |  |               |        |                    |        |  |  |                     |        | +------------------->      | |
| |       |  |               |        |                    |        |  |  |                     |        |   TCP                      | |
| | +-----+--+--+     +------v-----+  | +------------+     |        |  |  | +------------+  +---+----+---v+rewriter++  +------------+ | |
| | |           |     |            |  | |            |     |        |  |  | |            |  |        |              |  |            | | |
| | |  filter   |     |   filter   +---->   colo     <--------+     +-------->  filter   +--> adjust |   adjust     +-->   filter   | | |
| | |  mirror   |     | redirector |  | |  compare   |     |  |        |  | | redirector |  | ack    |   seq        |  | redirector | | |
| | |           |     |            |  | |            |     |  |        |  | |            |  |        |              |  |            | | |
| | +----^------+     +------------+  | +-----+------+     |  |        |  | +------------+  +--------+--------------+  +---+--------+ | |
| |      |     tx                 rx  |       |            |  |        |  |            tx                        all       |  rx      | |
| |      |                            |       |            |  |        |  +-----------------------------------------------------------+ |
| |      |                            |       |            |  |        |                                                   |            |
| |      |   filter excute order      |       |            |  |        |                                                   |            |
| |      |  +------------------->     |       |            |  +------------------------------------------------------------+            |
| +-----------------------------------+       |            |           |                                                                |
|        |                                    |            |           |                                                                |
+----------------------------------------------------------+           +----------------------------------------------------------------+
         |guest receive                       |guest send
         |                                    |
+--------+------------------------------------v------------+
|                                                          |                              NOTE: filter direction is rx/tx/all
|                         tap                              |                              rx:receive packets sent to the netdev
|                                                          |                              tx:receive packets sent by the netdev
+----------------------------------------------------------+

In COLO-compare.
Packets coming from the primary char indev will be sent to outdev Packets coming from the secondary char dev will be dropped colo-comapre need two input chardev and one output chardev:
primary_in=chardev1-id
secondary_in=chardev2-id
outdev=chardev3-id

usage:

primary:
-netdev tap,id=hn0,vhost=off,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -device e1000,id=e0,netdev=hn0,mac=52:a4:00:12:78:66
-chardev socket,id=mirror0,host=3.3.3.3,port=9003,server,nowait
-chardev socket,id=compare1,host=3.3.3.3,port=9004,server,nowait
-chardev socket,id=compare0,host=3.3.3.3,port=9001,server,nowait
-chardev socket,id=compare0-0,host=3.3.3.3,port=9001
-chardev socket,id=compare_out,host=3.3.3.3,port=9005,server,nowait
-chardev socket,id=compare_out0,host=3.3.3.3,port=9005
-object filter-mirror,id=m0,netdev=hn0,queue=tx,outdev=mirror0
-object filter-redirector,netdev=hn0,id=redire0,queue=rx,indev=compare_out -object filter-redirector,netdev=hn0,id=redire1,queue=rx,outdev=compare0 -object colo-compare,id=comp0,primary_in=compare0-0,secondary_in=compare1,outdev=compare_out0

secondary:
-netdev tap,id=hn0,vhost=off,script=/etc/qemu-ifup,down script=/etc/qemu-ifdown -device e1000,netdev=hn0,mac=52:a4:00:12:78:66
-chardev socket,id=red0,host=3.3.3.3,port=9003
-chardev socket,id=red1,host=3.3.3.3,port=9004
-object filter-redirector,id=f1,netdev=hn0,queue=tx,indev=red0 -object filter-redirector,id=f2,netdev=hn0,queue=rx,outdev=red1

---
 net/Makefile.objs  |   1 +
 net/colo-compare.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-options.hx    |  38 +++++++++
 vl.c               |   3 +-
 4 files changed, 263 insertions(+), 1 deletion(-)
 create mode 100644 net/colo-compare.c
net/Makefile.objs +1 -0
index b7c22fd..ba92f73 100644
@@ -16,3 +16,4 @@ common-obj-$(CONFIG_NETMAP) += netmap.o
 common-obj-y += filter.o
 common-obj-y += filter-buffer.o
 common-obj-y += filter-mirror.o
+common-obj-y += colo-compare.o
net/colo-compare.c new file +222 -0

222 lines not on this page · raw message

qemu-options.hx +38 -0
index 587de8f..79e5896 100644
@@ -3866,6 +3866,44 @@ Dump the network traffic on netdev @var{dev} to the file specified by
 The file format is libpcap, so it can be analyzed with tools such as tcpdump
 or Wireshark.
 
+@item -object colo-compare,id=@var{id},primary_in=@var{chardevid},secondary_in=@var{chardevid},
+outdev=@var{chardevid}
+
+Colo-compare gets packet from primary_in@var{chardevid} and secondary_in@var{chardevid},than compare primary packet with
+secondary packet. if the packet same,we will output primary
+packet to outdev@var{chardevid},else we will notify colo-frame
+do checkpoint and send primary packet to outdev@var{chardevid}.
+
+we can use it with the help of filter-mirror and filter-redirector.
+
+The simple usage:
+
+@example
+
+primary:
+-netdev tap,id=hn0,vhost=off,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown
+-device e1000,id=e0,netdev=hn0,mac=52:a4:00:12:78:66
+-chardev socket,id=mirror0,host=3.3.3.3,port=9003,server,nowait
+-chardev socket,id=compare1,host=3.3.3.3,port=9004,server,nowait
+-chardev socket,id=compare0,host=3.3.3.3,port=9001,server,nowait
+-chardev socket,id=compare0-0,host=3.3.3.3,port=9001
+-chardev socket,id=compare_out,host=3.3.3.3,port=9005,server,nowait
+-chardev socket,id=compare_out0,host=3.3.3.3,port=9005
+-object filter-mirror,id=m0,netdev=hn0,queue=tx,outdev=mirror0
+-object filter-redirector,netdev=hn0,id=redire0,queue=rx,indev=compare_out
+-object filter-redirector,netdev=hn0,id=redire1,queue=rx,outdev=compare0
+-object colo-compare,id=comp0,primary_in=compare0-0,secondary_in=compare1,outdev=compare_out0
+
+secondary:
+-netdev tap,id=hn0,vhost=off,script=/etc/qemu-ifup,down script=/etc/qemu-ifdown
+-device e1000,netdev=hn0,mac=52:a4:00:12:78:66
+-chardev socket,id=red0,host=3.3.3.3,port=9003
+-chardev socket,id=red1,host=3.3.3.3,port=9004
+-object filter-redirector,id=f1,netdev=hn0,queue=tx,indev=red0
+-object filter-redirector,id=f2,netdev=hn0,queue=rx,outdev=red1
+
+@end example
+
 @item -object secret,id=@var{id},data=@var{string},format=@var{raw|base64}[,keyid=@var{secretid},iv=@var{string}]
 @item -object secret,id=@var{id},file=@var{filename},format=@var{raw|base64}[,keyid=@var{secretid},iv=@var{string}]
 
vl.c +2 -1
index cbe51ac..c6b9a6f 100644
@@ -2865,7 +2865,8 @@ static bool object_create_initial(const char *type)
     if (g_str_equal(type, "filter-buffer") ||
         g_str_equal(type, "filter-dump") ||
         g_str_equal(type, "filter-mirror") ||
-        g_str_equal(type, "filter-redirector")) {
+        g_str_equal(type, "filter-redirector") ||
+        g_str_equal(type, "colo-compare")) {
         return false;
     }
 
Signature · 1 line
2.7.4
author
#2 lore raw

RFC PATCH v9 2/7 colo-base: add colo-base to define and handle packet

In reply to Zhang Chen

COLO-base used by colo-compare and filter-rewriter. this can share common data structure like:net packet, and share other functions.

---
 net/Makefile.objs  |   1 +
 net/colo-base.c    |  74 +++++++++++++++++++++++++++++++++
 net/colo-base.h    |  38 +++++++++++++++++
 net/colo-compare.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 trace-events       |   3 ++
 5 files changed, 233 insertions(+), 2 deletions(-)
 create mode 100644 net/colo-base.c
 create mode 100644 net/colo-base.h
net/Makefile.objs +1 -0
index ba92f73..119589f 100644
@@ -17,3 +17,4 @@ common-obj-y += filter.o
 common-obj-y += filter-buffer.o
 common-obj-y += filter-mirror.o
 common-obj-y += colo-compare.o
+common-obj-y += colo-base.o
net/colo-base.c new file +74 -0

74 lines not on this page · raw message

net/colo-base.h new file +38 -0
new file mode 100644
index 0000000..48835e7
@@ -0,0 +1,38 @@
+/*
+ * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO)
+ * (a.k.a. Fault Tolerance or Continuous Replication)
+ *
+ * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
+ * Copyright (c) 2016 FUJITSU LIMITED
+ * Copyright (c) 2016 Intel Corporation
+ *
+ * Author: Zhang Chen <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_COLO_BASE_H
+#define QEMU_COLO_BASE_H
+
+#include "slirp/slirp.h"
+#include "qemu/jhash.h"
+
+#define HASHTABLE_MAX_SIZE 16384
+
+typedef struct Packet {
+    void *data;
+    union {
+        uint8_t *network_layer;
+        struct ip *ip;
+    };
+    uint8_t *transport_layer;
+    int size;
+} Packet;
+
+int parse_packet_early(Packet *pkt);
+void connection_hashtable_reset(GHashTable *connection_track_table);
+Packet *packet_new(const void *data, int size);
+void packet_destroy(void *opaque, void *user_data);
+
+#endif /* QEMU_COLO_BASE_H */
net/colo-compare.c +117 -2

160 lines not on this page · raw message

trace-events +3 -0
index ca7211b..703de1a 100644
@@ -1916,3 +1916,6 @@ aspeed_vic_update_fiq(int flags) "Raising FIQ: %d"
 aspeed_vic_update_irq(int flags) "Raising IRQ: %d"
 aspeed_vic_read(uint64_t offset, unsigned size, uint32_t value) "From 0x%" PRIx64 " of size %u: 0x%" PRIx32
 aspeed_vic_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32
+
+# net/colo-compare.c
+colo_compare_main(const char *chr) ": %s"
Signature · 1 line
2.7.4
author
#3 lore raw

RFC PATCH v9 3/7 Jhash: add linux kernel jhashtable in qemu

In reply to Zhang Chen

Jhash used by colo-compare and filter-rewriter to save and lookup net connection info

---
 include/qemu/jhash.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 include/qemu/jhash.h
include/qemu/jhash.h new file +61 -0
new file mode 100644
index 0000000..0fcd875
@@ -0,0 +1,61 @@
+/* jhash.h: Jenkins hash support.
+  *
+  * Copyright (C) 2006. Bob Jenkins ([email protected])
+  *
+  * http://burtleburtle.net/bob/hash/
+  *
+  * These are the credits from Bob's sources:
+  *
+  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
+  *
+  * These are functions for producing 32-bit hashes for hash table lookup.
+  * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
+  * are externally useful functions.  Routines to test the hash are
+included
+  * if SELF_TEST is defined.  You can use this free for any purpose.
+It's in
+  * the public domain.  It has no warranty.
+  *
+  * Copyright (C) 2009-2010 Jozsef Kadlecsik ([email protected])
+  *
+  * I've modified Bob's hash to be useful in the Linux kernel, and
+  * any bugs present are my fault.
+  * Jozsef
+  */
+
+#ifndef QEMU_JHASH_H__
+#define QEMU_JHASH_H__
+
+#include "qemu/bitops.h"
+
+/*
+ * hashtable relation copy from linux kernel jhash
+ */
+
+/* __jhash_mix -- mix 3 32-bit values reversibly. */
+#define __jhash_mix(a, b, c)                \
+{                                           \
+    a -= c;  a ^= rol32(c, 4);  c += b;     \
+    b -= a;  b ^= rol32(a, 6);  a += c;     \
+    c -= b;  c ^= rol32(b, 8);  b += a;     \
+    a -= c;  a ^= rol32(c, 16); c += b;     \
+    b -= a;  b ^= rol32(a, 19); a += c;     \
+    c -= b;  c ^= rol32(b, 4);  b += a;     \
+}
+
+/* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
+#define __jhash_final(a, b, c)  \
+{                               \
+    c ^= b; c -= rol32(b, 14);  \
+    a ^= c; a -= rol32(c, 11);  \
+    b ^= a; b -= rol32(a, 25);  \
+    c ^= b; c -= rol32(b, 16);  \
+    a ^= c; a -= rol32(c, 4);   \
+    b ^= a; b -= rol32(a, 14);  \
+    c ^= b; c -= rol32(b, 24);  \
+}
+
+/* An arbitrary initial parameter */
+#define JHASH_INITVAL           0xdeadbeef
+
+#endif /* QEMU_JHASH_H__ */
Signature · 1 line
2.7.4
author
#4 lore raw

RFC PATCH v9 4/7 colo-compare: track connection and enqueue packet

In reply to Zhang Chen

In this patch we use kernel jhash table to track connection, and then enqueue net packet like this:

+ CompareState ++

|               |
+---------------+   +---------------+         +---------------+
|conn list      +--->conn           +--------->conn           |
+---------------+   +---------------+         +---------------+
|               |     |           |             |          |
+---------------+ +---v----+  +---v----+    +---v----+ +---v----+
                  |primary |  |secondary    |primary | |secondary
                  |packet  |  |packet  +    |packet  | |packet  +
                  +--------+  +--------+    +--------+ +--------+
                      |           |             |          |
                  +---v----+  +---v----+    +---v----+ +---v----+
                  |primary |  |secondary    |primary | |secondary
                  |packet  |  |packet  +    |packet  | |packet  +
                  +--------+  +--------+    +--------+ +--------+
                      |           |             |          |
                  +---v----+  +---v----+    +---v----+ +---v----+
                  |primary |  |secondary    |primary | |secondary
                  |packet  |  |packet  +    |packet  | |packet  +
                  +--------+  +--------+    +--------+ +--------+

We use conn_list to record connection info.
When we want to enqueue a packet, firstly get the connection from connection_track_table. then push the packet to g_queue(pri/sec) in it's own conn.

---
 net/colo-base.c    | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/colo-base.h    |  30 +++++++++++++++
 net/colo-compare.c |  70 +++++++++++++++++++++++++++++-----
 3 files changed, 198 insertions(+), 10 deletions(-)
net/colo-base.c +108 -0
index f5d5de9..7e91dec 100644
@@ -16,6 +16,29 @@
 #include "qemu/error-report.h"
 #include "net/colo-base.h"
 
+uint32_t connection_key_hash(const void *opaque)
+{
+    const ConnectionKey *key = opaque;
+    uint32_t a, b, c;
+
+    /* Jenkins hash */
+    a = b = c = JHASH_INITVAL + sizeof(*key);
+    a += key->src.s_addr;
+    b += key->dst.s_addr;
+    c += (key->src_port | key->dst_port << 16);
+    __jhash_mix(a, b, c);
+
+    a += key->ip_proto;
+    __jhash_final(a, b, c);
+
+    return c;
+}
+
+int connection_key_equal(const void *key1, const void *key2)
+{
+    return memcmp(key1, key2, sizeof(ConnectionKey)) == 0;
+}
+
 int parse_packet_early(Packet *pkt)
 {
     int network_length;
@@ -47,6 +70,62 @@ int parse_packet_early(Packet *pkt)
     return 0;
 }
 
+void fill_connection_key(Packet *pkt, ConnectionKey *key)
+{
+    uint32_t tmp_ports;
+
+    key->ip_proto = pkt->ip->ip_p;
+
+    switch (key->ip_proto) {
+    case IPPROTO_TCP:
+    case IPPROTO_UDP:
+    case IPPROTO_DCCP:
+    case IPPROTO_ESP:
+    case IPPROTO_SCTP:
+    case IPPROTO_UDPLITE:
+        tmp_ports = *(uint32_t *)(pkt->transport_layer);
+        key->src = pkt->ip->ip_src;
+        key->dst = pkt->ip->ip_dst;
+        key->src_port = ntohs(tmp_ports & 0xffff);
+        key->dst_port = ntohs(tmp_ports >> 16);
+        break;
+    case IPPROTO_AH:
+        tmp_ports = *(uint32_t *)(pkt->transport_layer + 4);
+        key->src = pkt->ip->ip_src;
+        key->dst = pkt->ip->ip_dst;
+        key->src_port = ntohs(tmp_ports & 0xffff);
+        key->dst_port = ntohs(tmp_ports >> 16);
+        break;
+    default:
+        key->src_port = 0;
+        key->dst_port = 0;
+        break;
+    }
+}
+
+Connection *connection_new(ConnectionKey *key)
+{
+    Connection *conn = g_slice_new(Connection);
+
+    conn->ip_proto = key->ip_proto;
+    conn->processing = false;
+    g_queue_init(&conn->primary_list);
+    g_queue_init(&conn->secondary_list);
+
+    return conn;
+}
+
+void connection_destroy(void *opaque)
+{
+    Connection *conn = opaque;
+
+    g_queue_foreach(&conn->primary_list, packet_destroy, NULL);
+    g_queue_free(&conn->primary_list);
+    g_queue_foreach(&conn->secondary_list, packet_destroy, NULL);
+    g_queue_free(&conn->secondary_list);
+    g_slice_free(Connection, conn);
+}
+
 Packet *packet_new(const void *data, int size)
 {
     Packet *pkt = g_slice_new(Packet);
@@ -72,3 +151,32 @@ void connection_hashtable_reset(GHashTable *connection_track_table)
 {
     g_hash_table_remove_all(connection_track_table);
 }
+
+/* if not found, create a new connection and add to hash table */
+Connection *connection_get(GHashTable *connection_track_table,
+                           ConnectionKey *key,
+                           uint32_t *hashtable_size)
+{
+    Connection *conn = g_hash_table_lookup(connection_track_table, key);
+
+    if (conn == NULL) {
+        ConnectionKey *new_key = g_memdup(key, sizeof(*key));
+
+        conn = connection_new(key);
+
+        (*hashtable_size) += 1;
+        if (*hashtable_size > HASHTABLE_MAX_SIZE) {
+            error_report("colo proxy connection hashtable full, clear it");
+            connection_hashtable_reset(connection_track_table);
+            /*
+             * when hashtable_size == 0, clear the conn_list
+             * in place where be called.
+             */
+            *hashtable_size = 0;
+        }
+
+        g_hash_table_insert(connection_track_table, new_key, conn);
+    }
+
+    return conn;
+}
net/colo-base.h +30 -0
index 48835e7..0505608 100644
@@ -30,7 +30,37 @@ typedef struct Packet {
     int size;
 } Packet;
 
+typedef struct ConnectionKey {
+    /* (src, dst) must be grouped, in the same way than in IP header */
+    struct in_addr src;
+    struct in_addr dst;
+    uint16_t src_port;
+    uint16_t dst_port;
+    uint8_t ip_proto;
+} QEMU_PACKED ConnectionKey;
+
+typedef struct Connection {
+    /* connection primary send queue: element type: Packet */
+    GQueue primary_list;
+    /* connection secondary send queue: element type: Packet */
+    GQueue secondary_list;
+    /* flag to enqueue unprocessed_connections */
+    bool processing;
+    uint8_t ip_proto;
+    /* be used by filter-rewriter */
+    tcp_seq  primary_seq;
+    tcp_seq  secondary_seq;
+} Connection;
+
+uint32_t connection_key_hash(const void *opaque);
+int connection_key_equal(const void *opaque1, const void *opaque2);
 int parse_packet_early(Packet *pkt);
+void fill_connection_key(Packet *pkt, ConnectionKey *key);
+Connection *connection_new(ConnectionKey *key);
+void connection_destroy(void *opaque);
+Connection *connection_get(GHashTable *connection_track_table,
+                           ConnectionKey *key,
+                           uint32_t *hashtable_size);
 void connection_hashtable_reset(GHashTable *connection_track_table);
 Packet *packet_new(const void *data, int size);
 void packet_destroy(void *opaque, void *user_data);
net/colo-compare.c +60 -10

111 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#5 lore raw

RFC PATCH v9 5/7 qemu-char: Add qemu_chr_add_handlers_full() for GMaincontext

In reply to Zhang Chen

Add qemu_chr_add_handlers_full() API, we can use this API pass in a GMainContext,make handler run in the context rather than main_loop.
This comments from Daniel P . Berrange.

Cc Daniel P . Berrange <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 include/sysemu/char.h |  11 ++++-
 qemu-char.c           | 119 +++++++++++++++++++++++++++++++-------------------
 2 files changed, 84 insertions(+), 46 deletions(-)
include/sysemu/char.h +10 -1
index 307fd8f..86888bc 100644
@@ -65,7 +65,8 @@ struct CharDriverState {
     int (*chr_sync_read)(struct CharDriverState *s,
                          const uint8_t *buf, int len);
     GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond);
-    void (*chr_update_read_handler)(struct CharDriverState *s);
+    void (*chr_update_read_handler_full)(struct CharDriverState *s,
+                                         GMainContext *context);
     int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
     int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
     int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
@@ -388,6 +389,14 @@ void qemu_chr_add_handlers(CharDriverState *s,
                            IOEventHandler *fd_event,
                            void *opaque);
 
+/* This API can make handler run in the context what you pass to. */
+void qemu_chr_add_handlers_full(CharDriverState *s,
+                                IOCanReadHandler *fd_can_read,
+                                IOReadHandler *fd_read,
+                                IOEventHandler *fd_event,
+                                void *opaque,
+                                GMainContext *context);
+
 void qemu_chr_be_generic_open(CharDriverState *s);
 void qemu_chr_accept_input(CharDriverState *s);
 int qemu_chr_add_client(CharDriverState *s, int fd);
qemu-char.c +74 -45

271 lines not on this page · raw message

Signature · 1 line
2.7.4
#6 lore raw

Re: [Qemu-devel] [RFC PATCH V9 5/7] qemu-char: Add qemu_chr_add_handlers_full() for GMaincontext

Quoted 188 lines · Zhang Chen

Add qemu_chr_add_handlers_full() API, we can use this API pass in a GMainContext,make handler run in the context rather than main_loop.
This comments from Daniel P . Berrange.

Cc Daniel P . Berrange <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
  include/sysemu/char.h |  11 ++++-
  qemu-char.c           | 119 +++++++++++++++++++++++++++++++-------------------
  2 files changed, 84 insertions(+), 46 deletions(-)
include/sysemu/char.h +10 -1
index 307fd8f..86888bc 100644
@@ -65,7 +65,8 @@ struct CharDriverState {
      int (*chr_sync_read)(struct CharDriverState *s,
                           const uint8_t *buf, int len);
      GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond);
-    void (*chr_update_read_handler)(struct CharDriverState *s);
+    void (*chr_update_read_handler_full)(struct CharDriverState *s,
+                                         GMainContext *context);
      int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
      int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
      int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
@@ -388,6 +389,14 @@ void qemu_chr_add_handlers(CharDriverState *s,
                             IOEventHandler *fd_event,
                             void *opaque);
 
+/* This API can make handler run in the context what you pass to. */
+void qemu_chr_add_handlers_full(CharDriverState *s,
+                                IOCanReadHandler *fd_can_read,
+                                IOReadHandler *fd_read,
+                                IOEventHandler *fd_event,
+                                void *opaque,
+                                GMainContext *context);
+
  void qemu_chr_be_generic_open(CharDriverState *s);
  void qemu_chr_accept_input(CharDriverState *s);
  int qemu_chr_add_client(CharDriverState *s, int fd);
qemu-char.c +39 -20
index b597ee1..0a45c9e 100644
@@ -448,11 +448,12 @@ void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
 
  static void remove_fd_in_watch(CharDriverState *chr);
 
-void qemu_chr_add_handlers(CharDriverState *s,
-                           IOCanReadHandler *fd_can_read,
-                           IOReadHandler *fd_read,
-                           IOEventHandler *fd_event,
-                           void *opaque)
+void qemu_chr_add_handlers_full(CharDriverState *s,
+                                IOCanReadHandler *fd_can_read,
+                                IOReadHandler *fd_read,
+                                IOEventHandler *fd_event,
+                                void *opaque,
+                                GMainContext *context)
  {
      int fe_open;
 
@@ -466,8 +467,9 @@ void qemu_chr_add_handlers(CharDriverState *s,
      s->chr_read = fd_read;
      s->chr_event = fd_event;
      s->handler_opaque = opaque;
-    if (fe_open && s->chr_update_read_handler)
-        s->chr_update_read_handler(s);
+    if (fe_open && s->chr_update_read_handler_full) {
+        s->chr_update_read_handler_full(s, context);
+    }
 
      if (!s->explicit_fe_open) {
          qemu_chr_fe_set_open(s, fe_open);
@@ -480,6 +482,16 @@ void qemu_chr_add_handlers(CharDriverState *s,
      }
  }
 
+void qemu_chr_add_handlers(CharDriverState *s,
+                           IOCanReadHandler *fd_can_read,
+                           IOReadHandler *fd_read,
+                           IOEventHandler *fd_event,
+                           void *opaque)
+{
+    qemu_chr_add_handlers_full(s, fd_can_read, fd_read,
+                               fd_event, opaque, NULL);
+}
+
  static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  {
      return len;
@@ -717,7 +729,8 @@ static void mux_chr_event(void *opaque, int event)
          mux_chr_send_event(d, i, event);
  }
 
-static void mux_chr_update_read_handler(CharDriverState *chr)
+static void mux_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      MuxDriver *d = chr->opaque;
 
@@ -731,8 +744,10 @@ static void mux_chr_update_read_handler(CharDriverState *chr)
      d->chr_event[d->mux_cnt] = chr->chr_event;
      /* Fix up the real driver with mux routines */
      if (d->mux_cnt == 0) {
-        qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
-                              mux_chr_event, chr);
+        qemu_chr_add_handlers_full(d->drv, mux_chr_can_read,
+                                   mux_chr_read,
+                                   mux_chr_event,
+                                   chr, context);
      }
      if (d->focus != -1) {
          mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
@@ -813,7 +828,7 @@ static CharDriverState *qemu_chr_open_mux(const char *id,
      d->drv = drv;
      d->focus = -1;
      chr->chr_write = mux_chr_write;
-    chr->chr_update_read_handler = mux_chr_update_read_handler;
+    chr->chr_update_read_handler_full = mux_chr_update_read_handler_full;
      chr->chr_accept_input = mux_chr_accept_input;
      /* Frontend guest-open / -close notification is not support with muxes */
      chr->chr_set_fe_open = NULL;
@@ -840,6 +855,7 @@ typedef struct IOWatchPoll
      IOCanReadHandler *fd_can_read;
      GSourceFunc fd_read;
      void *opaque;
+    GMainContext *context;
  } IOWatchPoll;
 
  static IOWatchPoll *io_watch_poll_from_source(GSource *source)
@@ -847,7 +863,8 @@ static IOWatchPoll *io_watch_poll_from_source(GSource *source)
      return container_of(source, IOWatchPoll, parent);
  }
 
-static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
+static gboolean io_watch_poll_prepare_full(GSource *source,
+                                            gint *timeout_)
  {
      IOWatchPoll *iwp = io_watch_poll_from_source(source);
      bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
@@ -860,7 +877,7 @@ static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
          iwp->src = qio_channel_create_watch(
              iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
          g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
-        g_source_attach(iwp->src, NULL);
+        g_source_attach(iwp->src, iwp->context);
      } else {
          g_source_destroy(iwp->src);
          g_source_unref(iwp->src);
@@ -896,33 +913,36 @@ static void io_watch_poll_finalize(GSource *source)
      assert(iwp->src == NULL);
  }
 
-static GSourceFuncs io_watch_poll_funcs = {
-    .prepare = io_watch_poll_prepare,
+static GSourceFuncs io_watch_poll_funcs_full = {
+    .prepare = io_watch_poll_prepare_full,
      .check = io_watch_poll_check,
      .dispatch = io_watch_poll_dispatch,
      .finalize = io_watch_poll_finalize,
  };
 
  /* Can only be used for read */
-static guint io_add_watch_poll(QIOChannel *ioc,
-                               IOCanReadHandler *fd_can_read,
-                               QIOChannelFunc fd_read,
-                               gpointer user_data)
+static guint io_add_watch_poll_full(QIOChannel *ioc,
+                                    IOCanReadHandler *fd_can_read,
+                                    QIOChannelFunc fd_read,
+                                    gpointer user_data,
+                                    GMainContext *context)
  {
      IOWatchPoll *iwp;
      int tag;
 
-    iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
+    iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs_full,
+                                       sizeof(IOWatchPoll));
      iwp->fd_can_read = fd_can_read;
      iwp->opaque = user_data;
      iwp->ioc = ioc;

qemu-char.c static void io_watch_poll_finalize(GSource *source) · line 945

Quoted 10 lines
qemu-char.c +3 -2
      iwp->fd_read = (GSourceFunc) fd_read;
      iwp->src = NULL;
+    iwp->context = context;
 
-    tag = g_source_attach(&iwp->parent, NULL);
+    tag = g_source_attach(&iwp->parent, context);
      g_source_unref(&iwp->parent);
      return tag;
-}
+ }

redundant modification

Thanks
Li Zhijian

Quoted 143 lines
qemu-char.c +32 -23
  static void io_remove_watch_poll(guint tag)
  {
@@ -1051,15 +1071,17 @@ static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
      return qio_channel_create_watch(s->ioc_out, cond);
  }
 
-static void fd_chr_update_read_handler(CharDriverState *chr)
+static void fd_chr_update_read_handler_full(CharDriverState *chr,
+                                            GMainContext *context)
  {
      FDCharDriver *s = chr->opaque;
 
      remove_fd_in_watch(chr);
      if (s->ioc_in) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc_in,
-                                           fd_chr_read_poll,
-                                           fd_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc_in,
+                                                fd_chr_read_poll,
+                                                fd_chr_read, chr,
+                                                context);
      }
  }
 
@@ -1098,7 +1120,7 @@ static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out,
      chr->opaque = s;
      chr->chr_add_watch = fd_chr_add_watch;
      chr->chr_write = fd_chr_write;
-    chr->chr_update_read_handler = fd_chr_update_read_handler;
+    chr->chr_update_read_handler_full = fd_chr_update_read_handler_full;
      chr->chr_close = fd_chr_close;
 
      return chr;
@@ -1303,7 +1325,8 @@ static void pty_chr_update_read_handler_locked(CharDriverState *chr)
      }
  }
 
-static void pty_chr_update_read_handler(CharDriverState *chr)
+static void pty_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      qemu_mutex_lock(&chr->chr_write_lock);
      pty_chr_update_read_handler_locked(chr);
@@ -1405,9 +1428,10 @@ static void pty_chr_state(CharDriverState *chr, int connected)
              s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr);
          }
          if (!chr->fd_in_tag) {
-            chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                               pty_chr_read_poll,
-                                               pty_chr_read, chr);
+            chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+                                                    pty_chr_read_poll,
+                                                    pty_chr_read,
+                                                    chr, NULL);
          }
      }
  }
@@ -1464,7 +1488,7 @@ static CharDriverState *qemu_chr_open_pty(const char *id,
      s = g_new0(PtyCharDriver, 1);
      chr->opaque = s;
      chr->chr_write = pty_chr_write;
-    chr->chr_update_read_handler = pty_chr_update_read_handler;
+    chr->chr_update_read_handler_full = pty_chr_update_read_handler_full;
      chr->chr_close = pty_chr_close;
      chr->chr_add_watch = pty_chr_add_watch;
      chr->explicit_be_open = true;
@@ -2546,15 +2570,17 @@ static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
      return TRUE;
  }
 
-static void udp_chr_update_read_handler(CharDriverState *chr)
+static void udp_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      NetCharDriver *s = chr->opaque;
 
      remove_fd_in_watch(chr);
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           udp_chr_read_poll,
-                                           udp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+                                                udp_chr_read_poll,
+                                                udp_chr_read, chr,
+                                                context);
      }
  }
 
@@ -2588,7 +2614,7 @@ static CharDriverState *qemu_chr_open_udp(QIOChannelSocket *sioc,
      s->bufptr = 0;
      chr->opaque = s;
      chr->chr_write = udp_chr_write;
-    chr->chr_update_read_handler = udp_chr_update_read_handler;
+    chr->chr_update_read_handler_full = udp_chr_update_read_handler_full;
      chr->chr_close = udp_chr_close;
      /* be isn't opened until we get a connection */
      chr->explicit_be_open = true;
@@ -2929,14 +2955,16 @@ static void tcp_chr_connect(void *opaque)
 
      s->connected = 1;
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           tcp_chr_read_poll,
-                                           tcp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+                                                tcp_chr_read_poll,
+                                                tcp_chr_read,
+                                                chr, NULL);
      }
      qemu_chr_be_generic_open(chr);
  }
 
-static void tcp_chr_update_read_handler(CharDriverState *chr)
+static void tcp_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      TCPCharDriver *s = chr->opaque;
 
@@ -2946,9 +2974,10 @@ static void tcp_chr_update_read_handler(CharDriverState *chr)
 
      remove_fd_in_watch(chr);
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           tcp_chr_read_poll,
-                                           tcp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+                                                tcp_chr_read_poll,
+                                                tcp_chr_read, chr,
+                                                context);
      }
  }
 
@@ -4409,7 +4438,7 @@ static CharDriverState *qmp_chardev_open_socket(const char *id,
      chr->set_msgfds = tcp_set_msgfds;
      chr->chr_add_client = tcp_chr_add_client;
      chr->chr_add_watch = tcp_chr_add_watch;
-    chr->chr_update_read_handler = tcp_chr_update_read_handler;
+    chr->chr_update_read_handler_full = tcp_chr_update_read_handler_full;
      /* be isn't opened until we get a connection */
      chr->explicit_be_open = true;
 
author
#7 lore raw

Re: [Qemu-devel] [RFC PATCH V9 5/7] qemu-char: Add qemu_chr_add_handlers_full() for GMaincontext

Quoted 208 lines · Li Zhijian
Quoted 205 lines · Zhang Chen

Add qemu_chr_add_handlers_full() API, we can use this API pass in a GMainContext,make handler run in the context rather than main_loop.
This comments from Daniel P . Berrange.

Cc Daniel P . Berrange <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
  include/sysemu/char.h |  11 ++++-
  qemu-char.c           | 119 
+++++++++++++++++++++++++++++++-------------------
  2 files changed, 84 insertions(+), 46 deletions(-)
include/sysemu/char.h +2 -1
index 307fd8f..86888bc 100644
@@ -65,7 +65,8 @@ struct CharDriverState {
      int (*chr_sync_read)(struct CharDriverState *s,
                           const uint8_t *buf, int len);
      GSource *(*chr_add_watch)(struct CharDriverState *s, 
 GIOCondition cond);
-    void (*chr_update_read_handler)(struct CharDriverState *s);
+    void (*chr_update_read_handler_full)(struct CharDriverState *s,
+                                         GMainContext *context);
      int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
      int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
      int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
include/sysemu/char.h +8 -0
@@ -388,6 +389,14 @@ void qemu_chr_add_handlers(CharDriverState *s,
                             IOEventHandler *fd_event,
                             void *opaque);
 
+/* This API can make handler run in the context what you pass to. */
+void qemu_chr_add_handlers_full(CharDriverState *s,
+                                IOCanReadHandler *fd_can_read,
+                                IOReadHandler *fd_read,
+                                IOEventHandler *fd_event,
+                                void *opaque,
+                                GMainContext *context);
+
  void qemu_chr_be_generic_open(CharDriverState *s);
  void qemu_chr_accept_input(CharDriverState *s);
  int qemu_chr_add_client(CharDriverState *s, int fd);
qemu-char.c +19 -7
index b597ee1..0a45c9e 100644
@@ -448,11 +448,12 @@ void qemu_chr_fe_printf(CharDriverState *s, 
 const char *fmt, ...)
 
  static void remove_fd_in_watch(CharDriverState *chr);
 
-void qemu_chr_add_handlers(CharDriverState *s,
-                           IOCanReadHandler *fd_can_read,
-                           IOReadHandler *fd_read,
-                           IOEventHandler *fd_event,
-                           void *opaque)
+void qemu_chr_add_handlers_full(CharDriverState *s,
+                                IOCanReadHandler *fd_can_read,
+                                IOReadHandler *fd_read,
+                                IOEventHandler *fd_event,
+                                void *opaque,
+                                GMainContext *context)
  {
      int fe_open;
@@ -466,8 +467,9 @@ void qemu_chr_add_handlers(CharDriverState *s,
      s->chr_read = fd_read;
      s->chr_event = fd_event;
      s->handler_opaque = opaque;
-    if (fe_open && s->chr_update_read_handler)
-        s->chr_update_read_handler(s);
+    if (fe_open && s->chr_update_read_handler_full) {
+        s->chr_update_read_handler_full(s, context);
+    }
 
      if (!s->explicit_fe_open) {
          qemu_chr_fe_set_open(s, fe_open);
@@ -480,6 +482,16 @@ void qemu_chr_add_handlers(CharDriverState *s,
      }
  }
 
+void qemu_chr_add_handlers(CharDriverState *s,
+                           IOCanReadHandler *fd_can_read,
+                           IOReadHandler *fd_read,
+                           IOEventHandler *fd_event,
+                           void *opaque)
+{
+    qemu_chr_add_handlers_full(s, fd_can_read, fd_read,
+                               fd_event, opaque, NULL);
+}
+
  static int null_chr_write(CharDriverState *chr, const uint8_t *buf, 
 int len)
  {
      return len;
qemu-char.c +6 -3
@@ -717,7 +729,8 @@ static void mux_chr_event(void *opaque, int event)
          mux_chr_send_event(d, i, event);
  }
 
-static void mux_chr_update_read_handler(CharDriverState *chr)
+static void mux_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      MuxDriver *d = chr->opaque;
 
@@ -731,8 +744,10 @@ static void 
 mux_chr_update_read_handler(CharDriverState *chr)
      d->chr_event[d->mux_cnt] = chr->chr_event;
      /* Fix up the real driver with mux routines */
      if (d->mux_cnt == 0) {
-        qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
-                              mux_chr_event, chr);
+        qemu_chr_add_handlers_full(d->drv, mux_chr_can_read,
+                                   mux_chr_read,
+                                   mux_chr_event,
+                                   chr, context);
      }
      if (d->focus != -1) {
          mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
qemu-char.c +1 -1
@@ -813,7 +828,7 @@ static CharDriverState *qemu_chr_open_mux(const 
 char *id,
      d->drv = drv;
      d->focus = -1;
      chr->chr_write = mux_chr_write;
-    chr->chr_update_read_handler = mux_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 mux_chr_update_read_handler_full;
      chr->chr_accept_input = mux_chr_accept_input;
      /* Frontend guest-open / -close notification is not support 
with muxes */
      chr->chr_set_fe_open = NULL;
qemu-char.c +3 -1
@@ -840,6 +855,7 @@ typedef struct IOWatchPoll
      IOCanReadHandler *fd_can_read;
      GSourceFunc fd_read;
      void *opaque;
+    GMainContext *context;
  } IOWatchPoll;
 
  static IOWatchPoll *io_watch_poll_from_source(GSource *source)
@@ -847,7 +863,8 @@ static IOWatchPoll 
 *io_watch_poll_from_source(GSource *source)
      return container_of(source, IOWatchPoll, parent);
  }
 
-static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
+static gboolean io_watch_poll_prepare_full(GSource *source,
+                                            gint *timeout_)
  {
      IOWatchPoll *iwp = io_watch_poll_from_source(source);
      bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
qemu-char.c +1 -1
@@ -860,7 +877,7 @@ static gboolean io_watch_poll_prepare(GSource 
 *source, gint *timeout_)
          iwp->src = qio_channel_create_watch(
              iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
          g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, 
 NULL);
-        g_source_attach(iwp->src, NULL);
+        g_source_attach(iwp->src, iwp->context);
      } else {
          g_source_destroy(iwp->src);
          g_source_unref(iwp->src);
qemu-char.c +10 -7
@@ -896,33 +913,36 @@ static void io_watch_poll_finalize(GSource 
 *source)
      assert(iwp->src == NULL);
  }
 
-static GSourceFuncs io_watch_poll_funcs = {
-    .prepare = io_watch_poll_prepare,
+static GSourceFuncs io_watch_poll_funcs_full = {
+    .prepare = io_watch_poll_prepare_full,
      .check = io_watch_poll_check,
      .dispatch = io_watch_poll_dispatch,
      .finalize = io_watch_poll_finalize,
  };
 
  /* Can only be used for read */
-static guint io_add_watch_poll(QIOChannel *ioc,
-                               IOCanReadHandler *fd_can_read,
-                               QIOChannelFunc fd_read,
-                               gpointer user_data)
+static guint io_add_watch_poll_full(QIOChannel *ioc,
+                                    IOCanReadHandler *fd_can_read,
+                                    QIOChannelFunc fd_read,
+                                    gpointer user_data,
+                                    GMainContext *context)
  {
      IOWatchPoll *iwp;
      int tag;
 
-    iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, 
 sizeof(IOWatchPoll));
+    iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs_full,
+                                       sizeof(IOWatchPoll));
      iwp->fd_can_read = fd_can_read;
      iwp->opaque = user_data;
      iwp->ioc = ioc;
      iwp->fd_read = (GSourceFunc) fd_read;
      iwp->src = NULL;
+    iwp->context = context;
 
Quoted 7 lines

qemu-char.c static void io_watch_poll_finalize(GSource *source) · line 945

Quoted 6 lines
qemu-char.c +2 -2
-    tag = g_source_attach(&iwp->parent, NULL);
+    tag = g_source_attach(&iwp->parent, context);
      g_source_unref(&iwp->parent);
      return tag;
-}
+ }

redundant modification

I miss it...
will fix in next version....

Should we make this patch independent with this patch serise?

Thanks
Zhang Chen

Quoted 163 lines

Thanks
Li Zhijian

Quoted 154 lines
qemu-char.c +0 -0
  static void io_remove_watch_poll(guint tag)
  {
qemu-char.c +7 -5
@@ -1051,15 +1071,17 @@ static GSource 
 *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
      return qio_channel_create_watch(s->ioc_out, cond);
  }
 
-static void fd_chr_update_read_handler(CharDriverState *chr)
+static void fd_chr_update_read_handler_full(CharDriverState *chr,
+                                            GMainContext *context)
  {
      FDCharDriver *s = chr->opaque;
 
      remove_fd_in_watch(chr);
      if (s->ioc_in) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc_in,
-                                           fd_chr_read_poll,
-                                           fd_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc_in,
+ fd_chr_read_poll,
+                                                fd_chr_read, chr,
+                                                context);
      }
  }
@@ -1098,7 +1120,7 @@ static CharDriverState *qemu_chr_open_fd(int 
 fd_in, int fd_out,
      chr->opaque = s;
      chr->chr_add_watch = fd_chr_add_watch;
      chr->chr_write = fd_chr_write;
-    chr->chr_update_read_handler = fd_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 fd_chr_update_read_handler_full;
      chr->chr_close = fd_chr_close;
      return chr;
qemu-char.c +2 -1
@@ -1303,7 +1325,8 @@ static void 
 pty_chr_update_read_handler_locked(CharDriverState *chr)
      }
  }
 
-static void pty_chr_update_read_handler(CharDriverState *chr)
+static void pty_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      qemu_mutex_lock(&chr->chr_write_lock);
      pty_chr_update_read_handler_locked(chr);
qemu-char.c +4 -3
@@ -1405,9 +1428,10 @@ static void pty_chr_state(CharDriverState 
 *chr, int connected)
              s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, 
 chr);
          }
          if (!chr->fd_in_tag) {
-            chr->fd_in_tag = io_add_watch_poll(s->ioc,
- pty_chr_read_poll,
-                                               pty_chr_read, chr);
+            chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+ pty_chr_read_poll,
+ pty_chr_read,
+                                                    chr, NULL);
          }
      }

}

qemu-char.c +1 -1
@@ -1464,7 +1488,7 @@ static CharDriverState *qemu_chr_open_pty(const 
 char *id,
      s = g_new0(PtyCharDriver, 1);
      chr->opaque = s;
      chr->chr_write = pty_chr_write;
-    chr->chr_update_read_handler = pty_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 pty_chr_update_read_handler_full;
      chr->chr_close = pty_chr_close;
      chr->chr_add_watch = pty_chr_add_watch;
      chr->explicit_be_open = true;
qemu-char.c +7 -5
@@ -2546,15 +2570,17 @@ static gboolean udp_chr_read(QIOChannel 
 *chan, GIOCondition cond, void *opaque)
      return TRUE;
  }
 
-static void udp_chr_update_read_handler(CharDriverState *chr)
+static void udp_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      NetCharDriver *s = chr->opaque;
 
      remove_fd_in_watch(chr);
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           udp_chr_read_poll,
-                                           udp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+ udp_chr_read_poll,
+                                                udp_chr_read, chr,
+                                                context);
      }
  }
@@ -2588,7 +2614,7 @@ static CharDriverState 
 *qemu_chr_open_udp(QIOChannelSocket *sioc,
      s->bufptr = 0;
      chr->opaque = s;
      chr->chr_write = udp_chr_write;
-    chr->chr_update_read_handler = udp_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 udp_chr_update_read_handler_full;
      chr->chr_close = udp_chr_close;
      /* be isn't opened until we get a connection */
      chr->explicit_be_open = true;
qemu-char.c +11 -8
@@ -2929,14 +2955,16 @@ static void tcp_chr_connect(void *opaque)
 
      s->connected = 1;
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           tcp_chr_read_poll,
-                                           tcp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+ tcp_chr_read_poll,
+                                                tcp_chr_read,
+                                                chr, NULL);
      }
      qemu_chr_be_generic_open(chr);
  }
 
-static void tcp_chr_update_read_handler(CharDriverState *chr)
+static void tcp_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      TCPCharDriver *s = chr->opaque;
 
@@ -2946,9 +2974,10 @@ static void 
 tcp_chr_update_read_handler(CharDriverState *chr)
 
      remove_fd_in_watch(chr);
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           tcp_chr_read_poll,
-                                           tcp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+ tcp_chr_read_poll,
+                                                tcp_chr_read, chr,
+                                                context);
      }
  }
@@ -4409,7 +4438,7 @@ static CharDriverState 
 *qmp_chardev_open_socket(const char *id,
      chr->set_msgfds = tcp_set_msgfds;
      chr->chr_add_client = tcp_chr_add_client;
      chr->chr_add_watch = tcp_chr_add_watch;
-    chr->chr_update_read_handler = tcp_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 tcp_chr_update_read_handler_full;
      /* be isn't opened until we get a connection */
Quoted 3 lines
      chr->explicit_be_open = true;

.

Signature · 2 lines
Thanks
zhangchen
author
#8 lore raw

Re: [Qemu-devel] [RFC PATCH V9 5/7] qemu-char: Add qemu_chr_add_handlers_full() for GMaincontext

add to: Daniel P . Berrange

Quoted 391 lines · Zhang Chen
Quoted 208 lines · Li Zhijian
Quoted 205 lines · Zhang Chen

Add qemu_chr_add_handlers_full() API, we can use this API pass in a GMainContext,make handler run in the context rather than main_loop.
This comments from Daniel P . Berrange.

Cc Daniel P . Berrange <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
  include/sysemu/char.h |  11 ++++-
  qemu-char.c           | 119 
+++++++++++++++++++++++++++++++-------------------
  2 files changed, 84 insertions(+), 46 deletions(-)
include/sysemu/char.h +2 -1
index 307fd8f..86888bc 100644
@@ -65,7 +65,8 @@ struct CharDriverState {
      int (*chr_sync_read)(struct CharDriverState *s,
                           const uint8_t *buf, int len);
      GSource *(*chr_add_watch)(struct CharDriverState *s, 
 GIOCondition cond);
-    void (*chr_update_read_handler)(struct CharDriverState *s);
+    void (*chr_update_read_handler_full)(struct CharDriverState *s,
+                                         GMainContext *context);
      int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
      int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
      int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
include/sysemu/char.h +8 -0
@@ -388,6 +389,14 @@ void qemu_chr_add_handlers(CharDriverState *s,
                             IOEventHandler *fd_event,
                             void *opaque);
 
+/* This API can make handler run in the context what you pass to. */
+void qemu_chr_add_handlers_full(CharDriverState *s,
+                                IOCanReadHandler *fd_can_read,
+                                IOReadHandler *fd_read,
+                                IOEventHandler *fd_event,
+                                void *opaque,
+                                GMainContext *context);
+
  void qemu_chr_be_generic_open(CharDriverState *s);
  void qemu_chr_accept_input(CharDriverState *s);
  int qemu_chr_add_client(CharDriverState *s, int fd);
qemu-char.c +19 -7
index b597ee1..0a45c9e 100644
@@ -448,11 +448,12 @@ void qemu_chr_fe_printf(CharDriverState *s, 
 const char *fmt, ...)
 
  static void remove_fd_in_watch(CharDriverState *chr);
 
-void qemu_chr_add_handlers(CharDriverState *s,
-                           IOCanReadHandler *fd_can_read,
-                           IOReadHandler *fd_read,
-                           IOEventHandler *fd_event,
-                           void *opaque)
+void qemu_chr_add_handlers_full(CharDriverState *s,
+                                IOCanReadHandler *fd_can_read,
+                                IOReadHandler *fd_read,
+                                IOEventHandler *fd_event,
+                                void *opaque,
+                                GMainContext *context)
  {
      int fe_open;
@@ -466,8 +467,9 @@ void qemu_chr_add_handlers(CharDriverState *s,
      s->chr_read = fd_read;
      s->chr_event = fd_event;
      s->handler_opaque = opaque;
-    if (fe_open && s->chr_update_read_handler)
-        s->chr_update_read_handler(s);
+    if (fe_open && s->chr_update_read_handler_full) {
+        s->chr_update_read_handler_full(s, context);
+    }
 
      if (!s->explicit_fe_open) {
          qemu_chr_fe_set_open(s, fe_open);
@@ -480,6 +482,16 @@ void qemu_chr_add_handlers(CharDriverState *s,
      }
  }
 
+void qemu_chr_add_handlers(CharDriverState *s,
+                           IOCanReadHandler *fd_can_read,
+                           IOReadHandler *fd_read,
+                           IOEventHandler *fd_event,
+                           void *opaque)
+{
+    qemu_chr_add_handlers_full(s, fd_can_read, fd_read,
+                               fd_event, opaque, NULL);
+}
+
  static int null_chr_write(CharDriverState *chr, const uint8_t 
 *buf, int len)
  {
      return len;
qemu-char.c +6 -3
@@ -717,7 +729,8 @@ static void mux_chr_event(void *opaque, int event)
          mux_chr_send_event(d, i, event);
  }
 
-static void mux_chr_update_read_handler(CharDriverState *chr)
+static void mux_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      MuxDriver *d = chr->opaque;
 
@@ -731,8 +744,10 @@ static void 
 mux_chr_update_read_handler(CharDriverState *chr)
      d->chr_event[d->mux_cnt] = chr->chr_event;
      /* Fix up the real driver with mux routines */
      if (d->mux_cnt == 0) {
-        qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
-                              mux_chr_event, chr);
+        qemu_chr_add_handlers_full(d->drv, mux_chr_can_read,
+                                   mux_chr_read,
+                                   mux_chr_event,
+                                   chr, context);
      }
      if (d->focus != -1) {
          mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
qemu-char.c +1 -1
@@ -813,7 +828,7 @@ static CharDriverState *qemu_chr_open_mux(const 
 char *id,
      d->drv = drv;
      d->focus = -1;
      chr->chr_write = mux_chr_write;
-    chr->chr_update_read_handler = mux_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 mux_chr_update_read_handler_full;
      chr->chr_accept_input = mux_chr_accept_input;
      /* Frontend guest-open / -close notification is not support 
with muxes */
      chr->chr_set_fe_open = NULL;
qemu-char.c +3 -1
@@ -840,6 +855,7 @@ typedef struct IOWatchPoll
      IOCanReadHandler *fd_can_read;
      GSourceFunc fd_read;
      void *opaque;
+    GMainContext *context;
  } IOWatchPoll;
 
  static IOWatchPoll *io_watch_poll_from_source(GSource *source)
@@ -847,7 +863,8 @@ static IOWatchPoll 
 *io_watch_poll_from_source(GSource *source)
      return container_of(source, IOWatchPoll, parent);
  }
 
-static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
+static gboolean io_watch_poll_prepare_full(GSource *source,
+                                            gint *timeout_)
  {
      IOWatchPoll *iwp = io_watch_poll_from_source(source);
      bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
qemu-char.c +1 -1
@@ -860,7 +877,7 @@ static gboolean io_watch_poll_prepare(GSource 
 *source, gint *timeout_)
          iwp->src = qio_channel_create_watch(
              iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
          g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, 
 NULL);
-        g_source_attach(iwp->src, NULL);
+        g_source_attach(iwp->src, iwp->context);
      } else {
          g_source_destroy(iwp->src);
          g_source_unref(iwp->src);
qemu-char.c +10 -7
@@ -896,33 +913,36 @@ static void io_watch_poll_finalize(GSource 
 *source)
      assert(iwp->src == NULL);
  }
 
-static GSourceFuncs io_watch_poll_funcs = {
-    .prepare = io_watch_poll_prepare,
+static GSourceFuncs io_watch_poll_funcs_full = {
+    .prepare = io_watch_poll_prepare_full,
      .check = io_watch_poll_check,
      .dispatch = io_watch_poll_dispatch,
      .finalize = io_watch_poll_finalize,
  };
 
  /* Can only be used for read */
-static guint io_add_watch_poll(QIOChannel *ioc,
-                               IOCanReadHandler *fd_can_read,
-                               QIOChannelFunc fd_read,
-                               gpointer user_data)
+static guint io_add_watch_poll_full(QIOChannel *ioc,
+                                    IOCanReadHandler *fd_can_read,
+                                    QIOChannelFunc fd_read,
+                                    gpointer user_data,
+                                    GMainContext *context)
  {
      IOWatchPoll *iwp;
      int tag;
 
-    iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, 
 sizeof(IOWatchPoll));
+    iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs_full,
+                                       sizeof(IOWatchPoll));
      iwp->fd_can_read = fd_can_read;
      iwp->opaque = user_data;
      iwp->ioc = ioc;
      iwp->fd_read = (GSourceFunc) fd_read;
      iwp->src = NULL;
+    iwp->context = context;
 
Quoted 7 lines

qemu-char.c static void io_watch_poll_finalize(GSource *source) · line 945

Quoted 6 lines
qemu-char.c +2 -2
-    tag = g_source_attach(&iwp->parent, NULL);
+    tag = g_source_attach(&iwp->parent, context);
      g_source_unref(&iwp->parent);
      return tag;
-}
+ }

redundant modification

I miss it...
will fix in next version....

Should we make this patch independent with this patch serise?

Thanks
Zhang Chen

Quoted 163 lines

Thanks
Li Zhijian

Quoted 154 lines
qemu-char.c +0 -0
  static void io_remove_watch_poll(guint tag)
  {
qemu-char.c +7 -5
@@ -1051,15 +1071,17 @@ static GSource 
 *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
      return qio_channel_create_watch(s->ioc_out, cond);
  }
 
-static void fd_chr_update_read_handler(CharDriverState *chr)
+static void fd_chr_update_read_handler_full(CharDriverState *chr,
+                                            GMainContext *context)
  {
      FDCharDriver *s = chr->opaque;
 
      remove_fd_in_watch(chr);
      if (s->ioc_in) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc_in,
-                                           fd_chr_read_poll,
-                                           fd_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc_in,
+ fd_chr_read_poll,
+                                                fd_chr_read, chr,
+                                                context);
      }
  }
@@ -1098,7 +1120,7 @@ static CharDriverState *qemu_chr_open_fd(int 
 fd_in, int fd_out,
      chr->opaque = s;
      chr->chr_add_watch = fd_chr_add_watch;
      chr->chr_write = fd_chr_write;
-    chr->chr_update_read_handler = fd_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 fd_chr_update_read_handler_full;
      chr->chr_close = fd_chr_close;
      return chr;
qemu-char.c +2 -1
@@ -1303,7 +1325,8 @@ static void 
 pty_chr_update_read_handler_locked(CharDriverState *chr)
      }
  }
 
-static void pty_chr_update_read_handler(CharDriverState *chr)
+static void pty_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      qemu_mutex_lock(&chr->chr_write_lock);
      pty_chr_update_read_handler_locked(chr);
qemu-char.c +4 -3
@@ -1405,9 +1428,10 @@ static void pty_chr_state(CharDriverState 
 *chr, int connected)
              s->open_tag = 
 g_idle_add(qemu_chr_be_generic_open_func, chr);
          }
          if (!chr->fd_in_tag) {
-            chr->fd_in_tag = io_add_watch_poll(s->ioc,
- pty_chr_read_poll,
-                                               pty_chr_read, chr);
+            chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+ pty_chr_read_poll,
+ pty_chr_read,
+                                                    chr, NULL);
          }
      }

}

qemu-char.c +1 -1
@@ -1464,7 +1488,7 @@ static CharDriverState 
 *qemu_chr_open_pty(const char *id,
      s = g_new0(PtyCharDriver, 1);
      chr->opaque = s;
      chr->chr_write = pty_chr_write;
-    chr->chr_update_read_handler = pty_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 pty_chr_update_read_handler_full;
      chr->chr_close = pty_chr_close;
      chr->chr_add_watch = pty_chr_add_watch;
      chr->explicit_be_open = true;
qemu-char.c +7 -5
@@ -2546,15 +2570,17 @@ static gboolean udp_chr_read(QIOChannel 
 *chan, GIOCondition cond, void *opaque)
      return TRUE;
  }
 
-static void udp_chr_update_read_handler(CharDriverState *chr)
+static void udp_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      NetCharDriver *s = chr->opaque;
 
      remove_fd_in_watch(chr);
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           udp_chr_read_poll,
-                                           udp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+ udp_chr_read_poll,
+                                                udp_chr_read, chr,
+                                                context);
      }
  }
@@ -2588,7 +2614,7 @@ static CharDriverState 
 *qemu_chr_open_udp(QIOChannelSocket *sioc,
      s->bufptr = 0;
      chr->opaque = s;
      chr->chr_write = udp_chr_write;
-    chr->chr_update_read_handler = udp_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 udp_chr_update_read_handler_full;
      chr->chr_close = udp_chr_close;
      /* be isn't opened until we get a connection */
      chr->explicit_be_open = true;
qemu-char.c +11 -8
@@ -2929,14 +2955,16 @@ static void tcp_chr_connect(void *opaque)
 
      s->connected = 1;
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           tcp_chr_read_poll,
-                                           tcp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+ tcp_chr_read_poll,
+                                                tcp_chr_read,
+                                                chr, NULL);
      }
      qemu_chr_be_generic_open(chr);
  }
 
-static void tcp_chr_update_read_handler(CharDriverState *chr)
+static void tcp_chr_update_read_handler_full(CharDriverState *chr,
+                                             GMainContext *context)
  {
      TCPCharDriver *s = chr->opaque;
 
@@ -2946,9 +2974,10 @@ static void 
 tcp_chr_update_read_handler(CharDriverState *chr)
 
      remove_fd_in_watch(chr);
      if (s->ioc) {
-        chr->fd_in_tag = io_add_watch_poll(s->ioc,
-                                           tcp_chr_read_poll,
-                                           tcp_chr_read, chr);
+        chr->fd_in_tag = io_add_watch_poll_full(s->ioc,
+ tcp_chr_read_poll,
+                                                tcp_chr_read, chr,
+                                                context);
      }
  }
@@ -4409,7 +4438,7 @@ static CharDriverState 
 *qmp_chardev_open_socket(const char *id,
      chr->set_msgfds = tcp_set_msgfds;
      chr->chr_add_client = tcp_chr_add_client;
      chr->chr_add_watch = tcp_chr_add_watch;
-    chr->chr_update_read_handler = tcp_chr_update_read_handler;
+    chr->chr_update_read_handler_full = 
 tcp_chr_update_read_handler_full;
      /* be isn't opened until we get a connection */
Quoted 3 lines
      chr->explicit_be_open = true;

.

Signature · 2 lines
Thanks
zhangchen
author
#9 lore raw

RFC PATCH v9 6/7 colo-compare: introduce packet comparison thread

In reply to Zhang Chen

If primary packet is same with secondary packet, we will send primary packet and drop secondary packet, otherwise notify COLO frame to do checkpoint. If primary packet comes and secondary packet not, after REGULAR_PACKET_CHECK_MS milliseconds we set the primary packet as old_packet,then do a checkpoint.

---
 net/colo-base.c    |   1 +
 net/colo-base.h    |   3 +
 net/colo-compare.c | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 trace-events       |   2 +
 4 files changed, 218 insertions(+)
net/colo-base.c +1 -0
index 7e91dec..eb1b631 100644
@@ -132,6 +132,7 @@ Packet *packet_new(const void *data, int size)
 
     pkt->data = g_memdup(data, size);
     pkt->size = size;
+    pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST);
 
     return pkt;
 }
net/colo-base.h +3 -0
index 0505608..06d6dca 100644
@@ -17,6 +17,7 @@
 
 #include "slirp/slirp.h"
 #include "qemu/jhash.h"
+#include "qemu/timer.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
@@ -28,6 +29,8 @@ typedef struct Packet {
     };
     uint8_t *transport_layer;
     int size;
+    /* Time of packet creation, in wall clock ms */
+    int64_t creation_ms;
 } Packet;
 
 typedef struct ConnectionKey {
net/colo-compare.c +212 -0

270 lines not on this page · raw message

trace-events +2 -0
index 703de1a..1537e91 100644
@@ -1919,3 +1919,5 @@ aspeed_vic_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64
 
 # net/colo-compare.c
 colo_compare_main(const char *chr) ": %s"
+colo_compare_ip_info(int psize, const char *sta, const char *stb, int ssize, const char *stc, const char *std) "ppkt size = %d, ip_src = %s, ip_dst = %s, spkt size = %d, ip_src = %s, ip_dst = %s"
+colo_old_packet_check_found(int64_t old_time) "%" PRId64
Signature · 1 line
2.7.4
author
#10 lore raw

RFC PATCH v9 7/7 colo-compare: add TCP, UDP, ICMP packet comparison

In reply to Zhang Chen

We add TCP,UDP,ICMP packet comparison to replace IP packet comparison. This can increase the accuracy of the package comparison.
less checkpoint more efficiency.

---
 net/colo-compare.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 trace-events       |   4 ++
 2 files changed, 174 insertions(+), 4 deletions(-)
net/colo-compare.c +170 -4
index e020edc..c7bb5f7 100644
@@ -18,6 +18,7 @@
 #include "qapi/qmp/qerror.h"
 #include "qapi/error.h"
 #include "net/net.h"
+#include "net/eth.h"
 #include "net/vhost_net.h"
 #include "qom/object_interfaces.h"
 #include "qemu/iov.h"
@@ -197,9 +198,158 @@ static int colo_packet_compare(Packet *ppkt, Packet *spkt)
     }
 }
 
-static int colo_packet_compare_all(Packet *spkt, Packet *ppkt)
+/*
+ * called from the compare thread on the primary
+ * for compare tcp packet
+ * compare_tcp copied from Dr. David Alan Gilbert's branch
+ */
+static int colo_packet_compare_tcp(Packet *spkt, Packet *ppkt)
+{
+    struct tcphdr *ptcp, *stcp;
+    int res;
+    char *sdebug, *ddebug;
+
+    trace_colo_compare_main("compare tcp");
+    if (ppkt->size != spkt->size) {
+        if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+            trace_colo_compare_main("pkt size not same");
+        }
+        return -1;
+    }
+
+    ptcp = (struct tcphdr *)ppkt->transport_layer;
+    stcp = (struct tcphdr *)spkt->transport_layer;
+
+    if (ptcp->th_seq != stcp->th_seq) {
+        if (trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+            trace_colo_compare_main("pkt tcp seq not same");
+        }
+        return -1;
+    }
+
+    /*
+     * The 'identification' field in the IP header is *very* random
+     * it almost never matches.  Fudge this by ignoring differences in
+     * unfragmented packets; they'll normally sort themselves out if different
+     * anyway, and it should recover at the TCP level.
+     * An alternative would be to get both the primary and secondary to rewrite
+     * somehow; but that would need some sync traffic to sync the state
+     */
+    if (ntohs(ppkt->ip->ip_off) & IP_DF) {
+        spkt->ip->ip_id = ppkt->ip->ip_id;
+        /* and the sum will be different if the IDs were different */
+        spkt->ip->ip_sum = ppkt->ip->ip_sum;
+    }
+
+    res = memcmp(ppkt->data + ETH_HLEN, spkt->data + ETH_HLEN,
+                (spkt->size - ETH_HLEN));
+
+    if (res != 0 && trace_event_get_state(TRACE_COLO_COMPARE_MISCOMPARE)) {
+        sdebug = strdup(inet_ntoa(ppkt->ip->ip_src));
+        ddebug = strdup(inet_ntoa(ppkt->ip->ip_dst));
+        fprintf(stderr, "%s: src/dst: %s/%s p: seq/ack=%u/%u"
+        " s: seq/ack=%u/%u res=%d flags=%x/%x\n", __func__,
+                   sdebug, ddebug,
+                   ntohl(ptcp->th_seq), ntohl(ptcp->th_ack),
+                   ntohl(stcp->th_seq), ntohl(stcp->th_ack),
+                   res, ptcp->th_flags, stcp->th_flags);
+
+        trace_colo_compare_tcp_miscompare("Primary len", ppkt->size);
+        qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", ppkt->size);
+        trace_colo_compare_tcp_miscompare("Secondary len", spkt->size);
+        qemu_hexdump((char *)spkt->data, stderr, "colo-compare", spkt->size);
+
+        g_free(sdebug);
+        g_free(ddebug);
+    }
+
+    return res;
+}
+
+/*
+ * called from the compare thread on the primary
+ * for compare udp packet
+ */
+static int colo_packet_compare_udp(Packet *spkt, Packet *ppkt)
+{
+    int ret;
+
+    trace_colo_compare_main("compare udp");
+    ret = colo_packet_compare(ppkt, spkt);
+
+    if (ret) {
+        trace_colo_compare_udp_miscompare("primary pkt size", ppkt->size);
+        qemu_hexdump((char *)ppkt->data, stderr, "colo-compare", ppkt->size);
+        trace_colo_compare_udp_miscompare("Secondary pkt size", spkt->size);
+        qemu_hexdump((char *)spkt->data, stderr, "colo-compare", spkt->size);
+    }
+
+    return ret;
+}
+
+/*
+ * called from the compare thread on the primary
+ * for compare icmp packet
+ */
+static int colo_packet_compare_icmp(Packet *spkt, Packet *ppkt)
 {
-    trace_colo_compare_main("compare all");
+    int network_length;
+    struct icmp *icmp_ppkt, *icmp_spkt;
+
+    trace_colo_compare_main("compare icmp");
+    network_length = ppkt->ip->ip_hl * 4;
+    if (ppkt->size != spkt->size ||
+        ppkt->size < network_length + ETH_HLEN) {
+        return -1;
+    }
+    icmp_ppkt = (struct icmp *)(ppkt->data + network_length + ETH_HLEN);
+    icmp_spkt = (struct icmp *)(spkt->data + network_length + ETH_HLEN);
+
+    if ((icmp_ppkt->icmp_type == icmp_spkt->icmp_type) &&
+        (icmp_ppkt->icmp_code == icmp_spkt->icmp_code)) {
+        if (icmp_ppkt->icmp_type == ICMP_REDIRECT) {
+            if (icmp_ppkt->icmp_gwaddr.s_addr !=
+                icmp_spkt->icmp_gwaddr.s_addr) {
+                trace_colo_compare_main("icmp_gwaddr.s_addr not same");
+                return -1;
+            }
+        } else if ((icmp_ppkt->icmp_type == ICMP_UNREACH) &&
+                   (icmp_ppkt->icmp_type == ICMP_UNREACH_NEEDFRAG)) {
+            if (icmp_ppkt->icmp_nextmtu != icmp_spkt->icmp_nextmtu) {
+                trace_colo_compare_main("icmp_nextmtu not same");
+                return -1;
+            }
+        }
+
+        if (colo_packet_compare(ppkt, spkt)) {
+            trace_colo_compare_icmp_miscompare("primary pkt size",
+                                               ppkt->size);
+            qemu_hexdump((char *)ppkt->data, stderr, "colo-compare",
+                         ppkt->size);
+            trace_colo_compare_icmp_miscompare("Secondary pkt size",
+                                               spkt->size);
+            qemu_hexdump((char *)spkt->data, stderr, "colo-compare",
+                         spkt->size);
+            return -1;
+        }
+    } else {
+        return -1;
+    }
+
+    return 0;
+}
+
+/*
+ * called from the compare thread on the primary
+ * for compare other packet
+ */
+static int colo_packet_compare_other(Packet *spkt, Packet *ppkt)
+{
+    trace_colo_compare_main("compare other");
+    trace_colo_compare_ip_info(ppkt->size, inet_ntoa(ppkt->ip->ip_src),
+                               inet_ntoa(ppkt->ip->ip_dst), spkt->size,
+                               inet_ntoa(spkt->ip->ip_src),
+                               inet_ntoa(spkt->ip->ip_dst));
     return colo_packet_compare(ppkt, spkt);
 }
 
@@ -263,8 +413,24 @@ static void colo_compare_connection(void *opaque, void *user_data)
     while (!g_queue_is_empty(&conn->primary_list) &&
            !g_queue_is_empty(&conn->secondary_list)) {
         pkt = g_queue_pop_tail(&conn->primary_list);
-        result = g_queue_find_custom(&conn->secondary_list,
-                              pkt, (GCompareFunc)colo_packet_compare_all);
+        switch (conn->ip_proto) {
+        case IPPROTO_TCP:
+            result = g_queue_find_custom(&conn->secondary_list,
+                     pkt, (GCompareFunc)colo_packet_compare_tcp);
+            break;
+        case IPPROTO_UDP:
+            result = g_queue_find_custom(&conn->secondary_list,
+                     pkt, (GCompareFunc)colo_packet_compare_udp);
+            break;
+        case IPPROTO_ICMP:
+            result = g_queue_find_custom(&conn->secondary_list,
+                     pkt, (GCompareFunc)colo_packet_compare_icmp);
+            break;
+        default:
+            result = g_queue_find_custom(&conn->secondary_list,
+                     pkt, (GCompareFunc)colo_packet_compare_other);
+            break;
+        }
 
         if (result) {
             ret = compare_chr_send(s->chr_out, pkt->data, pkt->size);
trace-events +4 -0
index 1537e91..ab22eb2 100644
@@ -1919,5 +1919,9 @@ aspeed_vic_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64
 
 # net/colo-compare.c
 colo_compare_main(const char *chr) ": %s"
+colo_compare_tcp_miscompare(const char *sta, int size) ": %s = %d"
+colo_compare_udp_miscompare(const char *sta, int size) ": %s = %d"
+colo_compare_icmp_miscompare(const char *sta, int size) ": %s = %d"
 colo_compare_ip_info(int psize, const char *sta, const char *stb, int ssize, const char *stc, const char *std) "ppkt size = %d, ip_src = %s, ip_dst = %s, spkt size = %d, ip_src = %s, ip_dst = %s"
 colo_old_packet_check_found(int64_t old_time) "%" PRId64
+colo_compare_miscompare(void) ""
Signature · 1 line
2.7.4