Skip to contentSkip to conversation

COLO: integrate colo frame with block replication and COLO proxy

PATCH In review Zhang Chen 3 participants 30 messages Sep 3, 2018 – Oct 24, 2018 Networking

lore
Share & tools
lore

Zhang Chen

author
#0 lore raw

v12 cover COLO: integrate colo frame with block replication and COLO proxy

Hi~ All~

COLO Frame, block replication and COLO proxy(colo-compare,filter-mirror, filter-redirector,filter-rewriter) have been exist in qemu
for long time, it's time to integrate these three parts to make COLO really works.

In this series, we have some optimizations for COLO frame, including separating the process of saving ram and device state, using an COLO_EXIT event to notify users that VM exits COLO, for these parts, most of them have been reviewed long time ago in old version, but since this series have just rebased on upstream which had merged a new series of migration, parts of pathes in this series deserve review again.

We use notifier/callback method for COLO compare to notify COLO frame about net packets inconsistent event, and add a handle_event method for NetFilterClass to help COLO frame to notify filters and colo-compare about checkpoint/failover event, it is flexible.

For the neweset version, please refer to:
https://github.com/zhangckid/qemu/tree/qemu-colo-18sep1

Please review, thanks.

V12:
- Rebased on upstream.
- Removed the patch 15/20 for feature work as Jason's comments.
- Fixed failover bugs in patch 16/19.
- Renamed the dummy_handle_event to default_handle_event in patch 15/19.
- Cleaned needless check job.

V11:
- Rebased on upstream.
- Used "RAMBLOCK_FOREACH_MIGRATABLE()" to replace "QLIST_FOREACH_RCU()" in patch 08/20.
- Fixed COLO related qapi command's since version in patch 10/20.

V10:
- Rebased on upstream.
- Removed the "active" in COLOState.
- Fixed some comments.

V9:
- Rebased on upstream codes.
- Addressed Jason's comments add TCP state machine track in filter-rewriter.
- Fix some bug in colo-compare.
- Fix typo.
- Add filter-rewriter failover handle.
- Add net client type check in colo-compare.
- Add COLO state diagram.
- Addressed Markus and Daive's comments.

V8:
- Rebased on upstream codes.
- Addressed Markus's comments in patch 10/17.
- Addressed Markus's comments in patch 11/17.
- Removed some comments in patch 4/17.
- Moved the "migration_bitmap_clear_dirty()" to suitable position in patch 9/17.
- Rewrote the patch 07/17 to address Davie's comments.
- Moved the "qemu_savevm_live_state" out of the qemu_mutex_lock_iothread.
- Fixed the bug that in some status COLO vm crash with segmentation fault.

V7:
- Addressed Markus's comments in 11/17.
- Rebased on upstream.

V6:
- Addressed Eric Blake's comments, use the enum to feedback in patch 11/17.
- Fixed QAPI command separator problem in patch 11/17.

Zhang Chen (15):
filter-rewriter: Add TCP state machine and fix memory leak in
connection_track_table
colo-compare: implement the process of checkpoint
colo-compare: use notifier to notify packets comparing result
COLO: integrate colo compare with colo frame
COLO: Add block replication into colo process
COLO: Remove colo_state migration struct
COLO: Load dirty pages into SVM's RAM cache firstly ram/COLO: Record the dirty pages that SVM received
COLO: Flush memory data from ram cache qapi/migration.json: Rename COLO unknown mode to none mode.
qapi: Add new command to query colo status
savevm: split the process of different stages for loadvm/savevm
filter: Add handle_event method for NetFilterClass
filter-rewriter: handle checkpoint and failover event
docs: Add COLO status diagram to COLO-FT.txt

zhanghailiang (4):
qmp event: Add COLO_EXIT event to notify users while exited COLO
COLO: flush host dirty ram from cache
COLO: notify net filters about checkpoint/failover event
COLO: quick failover process by kick COLO thread

 docs/COLO-FT.txt          |  34 ++++++
 include/exec/ram_addr.h   |   1 +
 include/migration/colo.h  |  11 +-
 include/net/filter.h      |   5 +
 migration/Makefile.objs   |   2 +-
 migration/colo-comm.c     |  76 --------------
 migration/colo-failover.c |   2 +-
 migration/colo.c          | 212 ++++++++++++++++++++++++++++++++++++--
 migration/migration.c     |  46 ++++++++-
 migration/ram.c           | 166 ++++++++++++++++++++++++++++-
 migration/ram.h           |   4 +
 migration/savevm.c        |  53 ++++++++--
 migration/savevm.h        |   5 +
 migration/trace-events    |   3 +
 net/colo-compare.c        | 115 +++++++++++++++++++--
 net/colo-compare.h        |  24 +++++
 net/colo.c                |  10 +-
 net/colo.h                |  11 +-
 net/filter-rewriter.c     | 162 +++++++++++++++++++++++++++--
 net/filter.c              |  17 +++
 net/net.c                 |  19 ++++
 qapi/migration.json       |  80 +++++++++++++-
 vl.c                      |   2 -
 23 files changed, 921 insertions(+), 139 deletions(-)
 delete mode 100644 migration/colo-comm.c
 create mode 100644 net/colo-compare.h
Signature · 1 line
2.17.GIT
author
#1 lore raw

PATCH v12 1/19 filter-rewriter: Add TCP state machine and fix memory leak in connection_track_table

We add almost full TCP state machine in filter-rewriter, except TCPS_LISTEN and some simplify in VM active close FIN states.

After a net connection is closed, we didn't clear its releated resources in connection_track_table, which will lead to memory leak.

Let't track the state of net connection, if it is closed, its related resources will be cleared up.

---
 net/colo.c            |   2 +-
 net/colo.h            |   9 ++--
 net/filter-rewriter.c | 105 ++++++++++++++++++++++++++++++++++++++----
 3 files changed, 100 insertions(+), 16 deletions(-)
net/colo.c +1 -1
index 6dda4ed66e..97c8fc928f 100644
@@ -137,7 +137,7 @@ Connection *connection_new(ConnectionKey *key)
     conn->ip_proto = key->ip_proto;
     conn->processing = false;
     conn->offset = 0;
-    conn->syn_flag = 0;
+    conn->tcp_state = TCPS_CLOSED;
     conn->pack = 0;
     conn->sack = 0;
     g_queue_init(&conn->primary_list);
net/colo.h +4 -5
index da6c36dcf7..0277e0e9ba 100644
@@ -18,6 +18,7 @@
 #include "slirp/slirp.h"
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
+#include "slirp/tcp.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
@@ -81,11 +82,9 @@ typedef struct Connection {
     uint32_t sack;
     /* offset = secondary_seq - primary_seq */
     tcp_seq  offset;
-    /*
-     * we use this flag update offset func
-     * run once in independent tcp connection
-     */
-    int syn_flag;
+
+    int tcp_state; /* TCP FSM state */
+    tcp_seq fin_ack_seq; /* the seq of 'fin=1,ack=1' */
 } Connection;
 
 uint32_t connection_key_hash(const void *opaque);
net/filter-rewriter.c +95 -10

175 lines not on this page · raw message

Signature · 1 line
2.17.GIT
#2 lore raw

Re: [Qemu-devel] [PATCH V12 01/19] filter-rewriter: Add TCP state machine and fix memory leak in connection_track_table

Quoted 2 lines · Zhang Chen

We add almost full TCP state machine in filter-rewriter, except TCPS_LISTEN and some simplify in VM active close FIN states.

Need to explain why it can be simplified.

Quoted 105 lines

After a net connection is closed, we didn't clear its releated resources in connection_track_table, which will lead to memory leak.

Let't track the state of net connection, if it is closed, its related resources will be cleared up.

---
  net/colo.c            |   2 +-
  net/colo.h            |   9 ++--
  net/filter-rewriter.c | 105 ++++++++++++++++++++++++++++++++++++++----
  3 files changed, 100 insertions(+), 16 deletions(-)
net/colo.c +1 -1
index 6dda4ed66e..97c8fc928f 100644
@@ -137,7 +137,7 @@ Connection *connection_new(ConnectionKey *key)
      conn->ip_proto = key->ip_proto;
      conn->processing = false;
      conn->offset = 0;
-    conn->syn_flag = 0;
+    conn->tcp_state = TCPS_CLOSED;
      conn->pack = 0;
      conn->sack = 0;
      g_queue_init(&conn->primary_list);
net/colo.h +4 -5
index da6c36dcf7..0277e0e9ba 100644
@@ -18,6 +18,7 @@
  #include "slirp/slirp.h"
  #include "qemu/jhash.h"
  #include "qemu/timer.h"
+#include "slirp/tcp.h"
 
  #define HASHTABLE_MAX_SIZE 16384
 
@@ -81,11 +82,9 @@ typedef struct Connection {
      uint32_t sack;
      /* offset = secondary_seq - primary_seq */
      tcp_seq  offset;
-    /*
-     * we use this flag update offset func
-     * run once in independent tcp connection
-     */
-    int syn_flag;
+
+    int tcp_state; /* TCP FSM state */
+    tcp_seq fin_ack_seq; /* the seq of 'fin=1,ack=1' */
  } Connection;
 
  uint32_t connection_key_hash(const void *opaque);
net/filter-rewriter.c +10 -5
index f584e4eba4..f18a71bf2e 100644
@@ -59,9 +59,9 @@ static int is_tcp_packet(Packet *pkt)
  }
 
  /* handle tcp packet from primary guest */
-static int handle_primary_tcp_pkt(NetFilterState *nf,
+static int handle_primary_tcp_pkt(RewriterState *rf,
                                    Connection *conn,
-                                  Packet *pkt)
+                                  Packet *pkt, ConnectionKey *key)
  {
      struct tcphdr *tcp_pkt;
 
@@ -74,23 +74,28 @@ static int handle_primary_tcp_pkt(NetFilterState *nf,
          trace_colo_filter_rewriter_conn_offset(conn->offset);
      }
 
+    if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN)) &&
+        conn->tcp_state == TCPS_SYN_SENT) {
+        conn->tcp_state = TCPS_ESTABLISHED;
+    }
+
      if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
          /*
           * we use this flag update offset func
           * run once in independent tcp connection
           */
-        conn->syn_flag = 1;
+        conn->tcp_state = TCPS_SYN_RECEIVED;
      }
 
      if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
-        if (conn->syn_flag) {
+        if (conn->tcp_state == TCPS_SYN_RECEIVED) {
              /*
               * offset = secondary_seq - primary seq
               * ack packet sent by guest from primary node,
               * so we use th_ack - 1 get primary_seq
               */
              conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
-            conn->syn_flag = 0;
+            conn->tcp_state = TCPS_ESTABLISHED;
          }
          if (conn->offset) {
              /* handle packets to the secondary from the primary */

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState *nf, · line 109

Quoted 6 lines
net/filter-rewriter.c +3 -0
@@ -99,15 +104,63 @@ static int handle_primary_tcp_pkt(NetFilterState *nf,
              net_checksum_calculate((uint8_t *)pkt->data + pkt->vnet_hdr_len,
                                     pkt->size - pkt->vnet_hdr_len);
          }
+        /*
+         * Case 1:
+         * Step 3:

Better have gathered comment instead of several scattered ones. This will make the code much more easier to be read.

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState *nf, · line 111

Quoted 2 lines
net/filter-rewriter.c +2 -0
+         * The *server* side of this connect is VM, *client* tries to close
+         * the connection.

Better use "passive close" here since:

- Server can active close the connection
- TCP allows distributed computing which does not differ much between server and client.

Quoted 24 lines
net/filter-rewriter.c +24 -0
+         *
+         * We got 'ack=1' packets from client side, it acks 'fin=1, ack=1'
+         * packet from server side. From this point, we can ensure that there
+         * will be no packets in the connection, except that, some errors
+         * happen between the path of 'filter object' and vNIC, if this rare
+         * case really happen, we can still create a new connection,
+         * So it is safe to remove the connection from connection_track_table.
+         *
+         */
+        if ((conn->tcp_state == TCPS_LAST_ACK) &&
+            (ntohl(tcp_pkt->th_ack) == (conn->fin_ack_seq + 1))) {
+            conn->tcp_state = TCPS_CLOSED;
+            g_hash_table_remove(rf->connection_track_table, key);
+        }
+    }
+
+    if ((tcp_pkt->th_flags & TH_FIN) == TH_FIN) {
+        /*
+         * Case 1:
+         * Step 1:
+         * The *server* side of this connect is VM, *client* tries to close
+         * the connection. We will into CLOSE_WAIT status.
+         */
+        if (conn->tcp_state == TCPS_ESTABLISHED) {

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState *nf, · line 145

Quoted 10 lines
net/filter-rewriter.c +10 -0
+            conn->tcp_state = TCPS_CLOSE_WAIT;
+        }
+
+        /*
+         * Case 2:
+         * Step 2:
+         * The *server* side of this connect is VM, *server* tries to close
+         * the connection. We will into CLOSE_WAIT status.
+         * We simplify the TCPS_FIN_WAIT_2, TCPS_TIME_WAIT and CLOSING status.
+         */

Better explain why you can do the simplification. E.g what happens if packets

Quoted 96 lines
net/filter-rewriter.c +46 -5
+        if (conn->tcp_state == TCPS_FIN_WAIT_1) {
+            conn->tcp_state = TCPS_TIME_WAIT;
+            /*
+             * For simplify implementation, we needn't wait 2MSL time
+             * in filter rewriter.
+             */
+            conn->tcp_state = TCPS_CLOSED;
+            g_hash_table_remove(rf->connection_track_table, key);
+        }
      }
 
      return 0;
  }
 
  /* handle tcp packet from secondary guest */
-static int handle_secondary_tcp_pkt(NetFilterState *nf,
+static int handle_secondary_tcp_pkt(RewriterState *rf,
                                      Connection *conn,
-                                    Packet *pkt)
+                                    Packet *pkt, ConnectionKey *key)
  {
      struct tcphdr *tcp_pkt;
 
@@ -121,7 +174,8 @@ static int handle_secondary_tcp_pkt(NetFilterState *nf,
          trace_colo_filter_rewriter_conn_offset(conn->offset);
      }
 
-    if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
+    if (conn->tcp_state == TCPS_SYN_RECEIVED &&
+        ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
          /*
           * save offset = secondary_seq and then
           * in handle_primary_tcp_pkt make offset
@@ -130,6 +184,12 @@ static int handle_secondary_tcp_pkt(NetFilterState *nf,
          conn->offset = ntohl(tcp_pkt->th_seq);
      }
 
+    /* VM active connect */
+    if (conn->tcp_state == TCPS_CLOSED &&
+        ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
+        conn->tcp_state = TCPS_SYN_SENT;
+    }
+
      if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
          /* Only need to adjust seq while offset is Non-zero */
          if (conn->offset) {
@@ -140,6 +200,31 @@ static int handle_secondary_tcp_pkt(NetFilterState *nf,
                                     pkt->size - pkt->vnet_hdr_len);
          }
      }
+    /*
+     * Case 1:
+     * Step 2:
+     * The *server* side of this connect is VM, *client* tries to close
+     * the connection. In this step we will into LAST_ACK status.
+     *
+     * We got 'fin=1, ack=1' packet from server side, we need to
+     * record the seq of 'fin=1, ack=1' packet.
+     */
+    if (conn->tcp_state == TCPS_CLOSE_WAIT &&
+        (tcp_pkt->th_flags & (TH_ACK | TH_FIN)) == (TH_ACK | TH_FIN)) {
+        conn->fin_ack_seq = ntohl(tcp_pkt->th_seq);
+        conn->tcp_state = TCPS_LAST_ACK;
+    }
+
+    /*
+     * Case 2:
+     * Step 1:
+     * The *server* side of this connect is VM, *server* tries to close
+     * the connection.
+     */
+    if (conn->tcp_state == TCPS_ESTABLISHED &&
+        (tcp_pkt->th_flags & (TH_ACK | TH_FIN)) == TH_FIN) {
+        conn->tcp_state = TCPS_FIN_WAIT_1;
+    }
 
      return 0;
  }
@@ -190,7 +275,7 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
 
          if (sender == nf->netdev) {
              /* NET_FILTER_DIRECTION_TX */
-            if (!handle_primary_tcp_pkt(nf, conn, pkt)) {
+            if (!handle_primary_tcp_pkt(s, conn, pkt, &key)) {
                  qemu_net_queue_send(s->incoming_queue, sender, 0,
                  (const uint8_t *)pkt->data, pkt->size, NULL);
                  packet_destroy(pkt, NULL);
@@ -203,7 +288,7 @@ static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
              }
          } else {
              /* NET_FILTER_DIRECTION_RX */
-            if (!handle_secondary_tcp_pkt(nf, conn, pkt)) {
+            if (!handle_secondary_tcp_pkt(s, conn, pkt, &key)) {
                  qemu_net_queue_send(s->incoming_queue, sender, 0,
                  (const uint8_t *)pkt->data, pkt->size, NULL);
                  packet_destroy(pkt, NULL);
author
#3 lore raw

Re: [Qemu-devel] [PATCH V12 01/19] filter-rewriter: Add TCP state machine and fix memory leak in connection_track_table

Quoted 8 lines · Jason Wang
Quoted 2 lines · Zhang Chen

We add almost full TCP state machine in filter-rewriter, except TCPS_LISTEN and some simplify in VM active close FIN states.

Need to explain why it can be simplified.

OK, I will note here like this : "We do this simplify job because guest kernel will track the TCP status and wait 2MSL time,
if client resend the FIN packet, guest will resend the last ACK too, so we needn't wait 2MSL time in filter-rewriter."

Quoted 110 lines
Quoted 74 lines

After a net connection is closed, we didn't clear its releated resources in connection_track_table, which will lead to memory leak.

Let't track the state of net connection, if it is closed, its related resources will be cleared up.

---
  net/colo.c            |   2 +-
  net/colo.h            |   9 ++--
  net/filter-rewriter.c | 105 ++++++++++++++++++++++++++++++++++++++----
  3 files changed, 100 insertions(+), 16 deletions(-)
net/colo.c +1 -1
index 6dda4ed66e..97c8fc928f 100644
@@ -137,7 +137,7 @@ Connection *connection_new(ConnectionKey *key)
      conn->ip_proto = key->ip_proto;
      conn->processing = false;
      conn->offset = 0;
-    conn->syn_flag = 0;
+    conn->tcp_state = TCPS_CLOSED;
      conn->pack = 0;
      conn->sack = 0;
      g_queue_init(&conn->primary_list);
net/colo.h +4 -5
index da6c36dcf7..0277e0e9ba 100644
@@ -18,6 +18,7 @@
  #include "slirp/slirp.h"
  #include "qemu/jhash.h"
  #include "qemu/timer.h"
+#include "slirp/tcp.h"
 
  #define HASHTABLE_MAX_SIZE 16384
 
@@ -81,11 +82,9 @@ typedef struct Connection {
      uint32_t sack;
      /* offset = secondary_seq - primary_seq */
      tcp_seq  offset;
-    /*
-     * we use this flag update offset func
-     * run once in independent tcp connection
-     */
-    int syn_flag;
+
+    int tcp_state; /* TCP FSM state */
+    tcp_seq fin_ack_seq; /* the seq of 'fin=1,ack=1' */
  } Connection;
 
  uint32_t connection_key_hash(const void *opaque);
net/filter-rewriter.c +2 -2
index f584e4eba4..f18a71bf2e 100644
@@ -59,9 +59,9 @@ static int is_tcp_packet(Packet *pkt)
  }
 
  /* handle tcp packet from primary guest */
-static int handle_primary_tcp_pkt(NetFilterState *nf,
+static int handle_primary_tcp_pkt(RewriterState *rf,
                                    Connection *conn,
-                                  Packet *pkt)
+                                  Packet *pkt, ConnectionKey *key)
  {
      struct tcphdr *tcp_pkt;
 

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState *nf, · line 77

Quoted 4 lines
net/filter-rewriter.c +1 -0
@@ -74,23 +74,28 @@ static int handle_primary_tcp_pkt(NetFilterState *nf,
          trace_colo_filter_rewriter_conn_offset(conn->offset);
      }
 
+    if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))

&&

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState

Quoted 28 lines
net/filter-rewriter.c +7 -3
+        conn->tcp_state == TCPS_SYN_SENT) {
+        conn->tcp_state = TCPS_ESTABLISHED;
+    }
+
      if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
          /*
           * we use this flag update offset func
           * run once in independent tcp connection
           */
-        conn->syn_flag = 1;
+        conn->tcp_state = TCPS_SYN_RECEIVED;
      }
 
      if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
-        if (conn->syn_flag) {
+        if (conn->tcp_state == TCPS_SYN_RECEIVED) {
              /*
               * offset = secondary_seq - primary seq
               * ack packet sent by guest from primary node,
               * so we use th_ack - 1 get primary_seq
               */
              conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
-            conn->syn_flag = 0;
+            conn->tcp_state = TCPS_ESTABLISHED;
          }
          if (conn->offset) {
              /* handle packets to the secondary from the primary */
@@ -99,15 +104,63 @@ static int handle_primary_tcp_pkt(NetFilterState

*nf,

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 104

Quoted 1 line
net/filter-rewriter.c +0 -0
              net_checksum_calculate((uint8_t *)pkt->data +

pkt->vnet_hdr_len,

Quoted 9 lines

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState *nf, · line 109

Quoted 5 lines
net/filter-rewriter.c +3 -0
                                     pkt->size - pkt->vnet_hdr_len);
          }
+        /*
+         * Case 1:
+         * Step 3:

Better have gathered comment instead of several scattered ones. This will make the code much more easier to be read.

OK, I will fix it and send one patch instead of this one.

Quoted 11 lines

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 110

Quoted 2 lines
net/filter-rewriter.c +1 -0
+         * The *server* side of this connect is VM, *client* tries to

close

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 111

Quoted 1 line
net/filter-rewriter.c +1 -0
+         * the connection.

Better use "passive close" here since:

- Server can active close the connection
- TCP allows distributed computing which does not differ much between server and client.

Yes. I got it.

Quoted 38 lines

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 113

Quoted 3 lines
net/filter-rewriter.c +2 -0
+         *
+         * We got 'ack=1' packets from client side, it acks 'fin=1,

ack=1'

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 114

Quoted 1 line
net/filter-rewriter.c +1 -0
+         * packet from server side. From this point, we can ensure that

there

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 115

Quoted 1 line
net/filter-rewriter.c +1 -0
+         * will be no packets in the connection, except that, some

errors

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 116

Quoted 1 line
net/filter-rewriter.c +1 -0
+         * happen between the path of 'filter object' and vNIC, if this

rare

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 118

Quoted 2 lines
net/filter-rewriter.c +2 -0
+         * case really happen, we can still create a new connection,
+         * So it is safe to remove the connection from

connection_track_table.

Quoted 4 lines
net/filter-rewriter.c +4 -0
+         *
+         */
+        if ((conn->tcp_state == TCPS_LAST_ACK) &&
+            (ntohl(tcp_pkt->th_ack) == (conn->fin_ack_seq + 1))) {

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState *nf,

Quoted 10 lines
net/filter-rewriter.c +10 -0
+            conn->tcp_state = TCPS_CLOSED;
+            g_hash_table_remove(rf->connection_track_table, key);
+        }
+    }
+
+    if ((tcp_pkt->th_flags & TH_FIN) == TH_FIN) {
+        /*
+         * Case 1:
+         * Step 1:
+         * The *server* side of this connect is VM, *client* tries to

close

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState *nf,

Quoted 10 lines
net/filter-rewriter.c +10 -0
+         * the connection. We will into CLOSE_WAIT status.
+         */
+        if (conn->tcp_state == TCPS_ESTABLISHED) {
+            conn->tcp_state = TCPS_CLOSE_WAIT;
+        }
+
+        /*
+         * Case 2:
+         * Step 2:
+         * The *server* side of this connect is VM, *server* tries to
Quoted 9 lines

close

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 144

Quoted 2 lines
net/filter-rewriter.c +2 -0
+         * the connection. We will into CLOSE_WAIT status.
+         * We simplify the TCPS_FIN_WAIT_2, TCPS_TIME_WAIT and CLOSING

status.

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState · line 145

Quoted 1 line
net/filter-rewriter.c +1 -0
+         */

Better explain why you can do the simplification. E.g what happens if packets

Yes, have been explained above.

Thanks
Zhang Chen

Quoted 106 lines

net/filter-rewriter.c static int handle_secondary_tcp_pkt(NetFilterState

Quoted 25 lines
net/filter-rewriter.c +11 -2
+        if (conn->tcp_state == TCPS_FIN_WAIT_1) {
+            conn->tcp_state = TCPS_TIME_WAIT;
+            /*
+             * For simplify implementation, we needn't wait 2MSL time
+             * in filter rewriter.
+             */
+            conn->tcp_state = TCPS_CLOSED;
+            g_hash_table_remove(rf->connection_track_table, key);
+        }
      }
 
      return 0;
  }
 
  /* handle tcp packet from secondary guest */
-static int handle_secondary_tcp_pkt(NetFilterState *nf,
+static int handle_secondary_tcp_pkt(RewriterState *rf,
                                      Connection *conn,
-                                    Packet *pkt)
+                                    Packet *pkt, ConnectionKey *key)
  {
      struct tcphdr *tcp_pkt;
 
@@ -121,7 +174,8 @@ static int handle_secondary_tcp_pkt(NetFilterState

*nf,

net/filter-rewriter.c static int handle_primary_tcp_pkt(NetFilterState *nf,

Quoted 4 lines
net/filter-rewriter.c +0 -1
          trace_colo_filter_rewriter_conn_offset(conn->offset);
      }
 
-    if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN)))

{

net/filter-rewriter.c static int handle_secondary_tcp_pkt(NetFilterState · line 178

Quoted 2 lines
net/filter-rewriter.c +2 -0
+    if (conn->tcp_state == TCPS_SYN_RECEIVED &&
+        ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN)))

{

net/filter-rewriter.c static int handle_secondary_tcp_pkt(NetFilterState

Quoted 4 lines
net/filter-rewriter.c +0 -0
          /*
           * save offset = secondary_seq and then
           * in handle_primary_tcp_pkt make offset
@@ -130,6 +184,12 @@ static int handle_secondary_tcp_pkt(NetFilterState

*nf,

net/filter-rewriter.c static int handle_secondary_tcp_pkt(NetFilterState

Quoted 13 lines
net/filter-rewriter.c +6 -0
          conn->offset = ntohl(tcp_pkt->th_seq);
      }
 
+    /* VM active connect */
+    if (conn->tcp_state == TCPS_CLOSED &&
+        ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
+        conn->tcp_state = TCPS_SYN_SENT;
+    }
+
      if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
          /* Only need to adjust seq while offset is Non-zero */
          if (conn->offset) {
@@ -140,6 +200,31 @@ static int handle_secondary_tcp_pkt(NetFilterState

*nf,

net/filter-rewriter.c static ssize_t

Quoted 32 lines
net/filter-rewriter.c +25 -0
                                     pkt->size - pkt->vnet_hdr_len);
          }
      }
+    /*
+     * Case 1:
+     * Step 2:
+     * The *server* side of this connect is VM, *client* tries to close
+     * the connection. In this step we will into LAST_ACK status.
+     *
+     * We got 'fin=1, ack=1' packet from server side, we need to
+     * record the seq of 'fin=1, ack=1' packet.
+     */
+    if (conn->tcp_state == TCPS_CLOSE_WAIT &&
+        (tcp_pkt->th_flags & (TH_ACK | TH_FIN)) == (TH_ACK | TH_FIN)) {
+        conn->fin_ack_seq = ntohl(tcp_pkt->th_seq);
+        conn->tcp_state = TCPS_LAST_ACK;
+    }
+
+    /*
+     * Case 2:
+     * Step 1:
+     * The *server* side of this connect is VM, *server* tries to close
+     * the connection.
+     */
+    if (conn->tcp_state == TCPS_ESTABLISHED &&
+        (tcp_pkt->th_flags & (TH_ACK | TH_FIN)) == TH_FIN) {
+        conn->tcp_state = TCPS_FIN_WAIT_1;
+    }
 
      return 0;
  }
@@ -190,7 +275,7 @@ static ssize_t

colo_rewriter_receive_iov(NetFilterState *nf,

net/filter-rewriter.c static ssize_t

Quoted 9 lines
net/filter-rewriter.c +1 -1
          if (sender == nf->netdev) {
              /* NET_FILTER_DIRECTION_TX */
-            if (!handle_primary_tcp_pkt(nf, conn, pkt)) {
+            if (!handle_primary_tcp_pkt(s, conn, pkt, &key)) {
                  qemu_net_queue_send(s->incoming_queue, sender, 0,
                  (const uint8_t *)pkt->data, pkt->size, NULL);
                  packet_destroy(pkt, NULL);
@@ -203,7 +288,7 @@ static ssize_t

colo_rewriter_receive_iov(NetFilterState *nf,

Quoted 8 lines
net/filter-rewriter.c +1 -1
              }
          } else {
              /* NET_FILTER_DIRECTION_RX */
-            if (!handle_secondary_tcp_pkt(nf, conn, pkt)) {
+            if (!handle_secondary_tcp_pkt(s, conn, pkt, &key)) {
                  qemu_net_queue_send(s->incoming_queue, sender, 0,
                  (const uint8_t *)pkt->data, pkt->size, NULL);
                  packet_destroy(pkt, NULL);
author
#4 lore raw

PATCH v12 2/19 colo-compare: implement the process of checkpoint

In reply to Zhang Chen

While do checkpoint, we need to flush all the unhandled packets, By using the filter notifier mechanism, we can easily to notify every compare object to do this process, which runs inside of compare threads as a coroutine.

---
 include/migration/colo.h |  6 ++++
 net/colo-compare.c       | 78 ++++++++++++++++++++++++++++++++++++++++
 net/colo-compare.h       | 22 ++++++++++++
 3 files changed, 106 insertions(+)
 create mode 100644 net/colo-compare.h
include/migration/colo.h +6 -0
index 2fe48ad353..fefb2fcf4c 100644
@@ -16,6 +16,12 @@
 #include "qemu-common.h"
 #include "qapi/qapi-types-migration.h"
 
+enum colo_event {
+    COLO_EVENT_NONE,
+    COLO_EVENT_CHECKPOINT,
+    COLO_EVENT_FAILOVER,
+};
+
 void colo_info_init(void);
 
 void migrate_start_colo_process(MigrationState *s);
net/colo-compare.c +78 -0

145 lines not on this page · raw message

net/colo-compare.h new file +22 -0
new file mode 100644
index 0000000000..1b1ce76aea
@@ -0,0 +1,22 @@
+/*
+ * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO)
+ * (a.k.a. Fault Tolerance or Continuous Replication)
+ *
+ * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
+ * Copyright (c) 2017 FUJITSU LIMITED
+ * Copyright (c) 2017 Intel Corporation
+ *
+ * Authors:
+ *    zhanghailiang <[email protected]>
+ *    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_COMPARE_H
+#define QEMU_COLO_COMPARE_H
+
+void colo_notify_compares_event(void *opaque, int event, Error **errp);
+
+#endif /* QEMU_COLO_COMPARE_H */
Signature · 1 line
2.17.GIT
author
#5 lore raw

PATCH v12 3/19 colo-compare: use notifier to notify packets comparing result

In reply to Zhang Chen

It's a good idea to use notifier to notify COLO frame of inconsistent packets comparing.

---
 net/colo-compare.c | 37 ++++++++++++++++++++++++++-----------
 net/colo-compare.h |  2 ++
 2 files changed, 28 insertions(+), 11 deletions(-)
net/colo-compare.c +26 -11
index 80e6532e8b..426eab5973 100644
@@ -29,6 +29,7 @@
 #include "sysemu/iothread.h"
 #include "net/colo-compare.h"
 #include "migration/colo.h"
+#include "migration/migration.h"
 
 #define TYPE_COLO_COMPARE "colo-compare"
 #define COLO_COMPARE(obj) \
@@ -37,6 +38,9 @@
 static QTAILQ_HEAD(, CompareState) net_compares =
        QTAILQ_HEAD_INITIALIZER(net_compares);
 
+static NotifierList colo_compare_notifiers =
+    NOTIFIER_LIST_INITIALIZER(colo_compare_notifiers);
+
 #define COMPARE_READ_LEN_MAX NET_BUFSIZE
 #define MAX_QUEUE_SIZE 1024
 
@@ -427,10 +431,7 @@ sec:
         qemu_hexdump((char *)spkt->data, stderr,
                      "colo-compare spkt", spkt->size);
 
-        /*
-         * colo_compare_inconsistent_notify();
-         * TODO: notice to checkpoint();
-         */
+        colo_compare_inconsistency_notify();
     }
 }
 
@@ -561,8 +562,24 @@ static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
     }
 }
 
+static void colo_compare_inconsistency_notify(void)
+{
+    notifier_list_notify(&colo_compare_notifiers,
+                migrate_get_current());
+}
+
+void colo_compare_register_notifier(Notifier *notify)
+{
+    notifier_list_add(&colo_compare_notifiers, notify);
+}
+
+void colo_compare_unregister_notifier(Notifier *notify)
+{
+    notifier_remove(notify);
+}
+
 static int colo_old_packet_check_one_conn(Connection *conn,
-                                          void *user_data)
+                                           void *user_data)
 {
     GList *result = NULL;
     int64_t check_time = REGULAR_PACKET_CHECK_MS;
@@ -573,10 +590,7 @@ static int colo_old_packet_check_one_conn(Connection *conn,
 
     if (result) {
         /* Do checkpoint will flush old packet */
-        /*
-         * TODO: Notify colo frame to do checkpoint.
-         * colo_compare_inconsistent_notify();
-         */
+        colo_compare_inconsistency_notify();
         return 0;
     }
 
@@ -620,11 +634,12 @@ static void colo_compare_packet(CompareState *s, Connection *conn,
             /*
              * If one packet arrive late, the secondary_list or
              * primary_list will be empty, so we can't compare it
-             * until next comparison.
+             * until next comparison. If the packets in the list are
+             * timeout, it will trigger a checkpoint request.
              */
             trace_colo_compare_main("packet different");
             g_queue_push_head(&conn->primary_list, pkt);
-            /* TODO: colo_notify_checkpoint();*/
+            colo_compare_inconsistency_notify();
             break;
         }
     }
net/colo-compare.h +2 -0
index 1b1ce76aea..22ddd512e2 100644
@@ -18,5 +18,7 @@
 #define QEMU_COLO_COMPARE_H
 
 void colo_notify_compares_event(void *opaque, int event, Error **errp);
+void colo_compare_register_notifier(Notifier *notify);
+void colo_compare_unregister_notifier(Notifier *notify);
 
 #endif /* QEMU_COLO_COMPARE_H */
Signature · 1 line
2.17.GIT
author
#6 lore raw

PATCH v12 4/19 COLO: integrate colo compare with colo frame

In reply to Zhang Chen

For COLO FT, both the PVM and SVM run at the same time, only sync the state while it needs.

So here, let SVM runs while not doing checkpoint, change DEFAULT_MIGRATE_X_CHECKPOINT_DELAY to 200*100.

Besides, we forgot to release colo_checkpoint_semd and colo_delay_timer, fix them here.

---
 migration/colo.c      | 42 ++++++++++++++++++++++++++++++++++++++++--
 migration/migration.c |  6 ++----
 2 files changed, 42 insertions(+), 6 deletions(-)
migration/colo.c +40 -2
index 88936f5962..f4bdfde170 100644
@@ -25,8 +25,11 @@
 #include "qemu/error-report.h"
 #include "migration/failover.h"
 #include "replication.h"
+#include "net/colo-compare.h"
+#include "net/colo.h"
 
 static bool vmstate_loading;
+static Notifier packets_compare_notifier;
 
 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
 
@@ -343,6 +346,11 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
         goto out;
     }
 
+    colo_notify_compares_event(NULL, COLO_EVENT_CHECKPOINT, &local_err);
+    if (local_err) {
+        goto out;
+    }
+
     /* Disable block migration */
     migrate_set_block_enabled(false, &local_err);
     qemu_savevm_state_header(fb);
@@ -400,6 +408,11 @@ out:
     return ret;
 }
 
+static void colo_compare_notify_checkpoint(Notifier *notifier, void *data)
+{
+    colo_checkpoint_notify(data);
+}
+
 static void colo_process_checkpoint(MigrationState *s)
 {
     QIOChannelBuffer *bioc;
@@ -416,6 +429,9 @@ static void colo_process_checkpoint(MigrationState *s)
         goto out;
     }
 
+    packets_compare_notifier.notify = colo_compare_notify_checkpoint;
+    colo_compare_register_notifier(&packets_compare_notifier);
+
     /*
      * Wait for Secondary finish loading VM states and enter COLO
      * restore.
@@ -461,11 +477,21 @@ out:
         qemu_fclose(fb);
     }
 
-    timer_del(s->colo_delay_timer);
-
     /* Hope this not to be too long to wait here */
     qemu_sem_wait(&s->colo_exit_sem);
     qemu_sem_destroy(&s->colo_exit_sem);
+
+    /*
+     * It is safe to unregister notifier after failover finished.
+     * Besides, colo_delay_timer and colo_checkpoint_sem can't be
+     * released befor unregister notifier, or there will be use-after-free
+     * error.
+     */
+    colo_compare_unregister_notifier(&packets_compare_notifier);
+    timer_del(s->colo_delay_timer);
+    timer_free(s->colo_delay_timer);
+    qemu_sem_destroy(&s->colo_checkpoint_sem);
+
     /*
      * Must be called after failover BH is completed,
      * Or the failover BH may shutdown the wrong fd that
@@ -559,6 +585,11 @@ void *colo_process_incoming_thread(void *opaque)
     fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
     object_unref(OBJECT(bioc));
 
+    qemu_mutex_lock_iothread();
+    vm_start();
+    trace_colo_vm_state_change("stop", "run");
+    qemu_mutex_unlock_iothread();
+
     colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
                       &local_err);
     if (local_err) {
@@ -578,6 +609,11 @@ void *colo_process_incoming_thread(void *opaque)
             goto out;
         }
 
+        qemu_mutex_lock_iothread();
+        vm_stop_force_state(RUN_STATE_COLO);
+        trace_colo_vm_state_change("run", "stop");
+        qemu_mutex_unlock_iothread();
+
         /* FIXME: This is unnecessary for periodic checkpoint mode */
         colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
                      &local_err);
@@ -631,6 +667,8 @@ void *colo_process_incoming_thread(void *opaque)
         }
 
         vmstate_loading = false;
+        vm_start();
+        trace_colo_vm_state_change("stop", "run");
         qemu_mutex_unlock_iothread();
 
         if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
migration/migration.c +2 -4
index 4b316ec343..2f3e9d40d1 100644
@@ -76,10 +76,8 @@
 /* Migration XBZRLE default cache size */
 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
 
-/* The delay time (in ms) between two COLO checkpoints
- * Note: Please change this default value to 10000 when we support hybrid mode.
- */
-#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200
+/* The delay time (in ms) between two COLO checkpoints */
+#define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
 #define DEFAULT_MIGRATE_MULTIFD_PAGE_COUNT 16
 
Signature · 1 line
2.17.GIT
author
#7 lore raw

PATCH v12 5/19 COLO: Add block replication into colo process

In reply to Zhang Chen

Make sure master start block replication after slave's block replication started.

Besides, we need to activate VM's blocks before goes into COLO state.

---
 migration/colo.c      | 43 +++++++++++++++++++++++++++++++++++++++++++
 migration/migration.c | 10 ++++++++++
 2 files changed, 53 insertions(+)
migration/colo.c +43 -0
index f4bdfde170..af04010061 100644
@@ -27,6 +27,7 @@
 #include "replication.h"
 #include "net/colo-compare.h"
 #include "net/colo.h"
+#include "block/block.h"
 
 static bool vmstate_loading;
 static Notifier packets_compare_notifier;
@@ -56,6 +57,7 @@ static void secondary_vm_do_failover(void)
 {
     int old_state;
     MigrationIncomingState *mis = migration_incoming_get_current();
+    Error *local_err = NULL;
 
     /* Can not do failover during the process of VM's loading VMstate, Or
      * it will break the secondary VM.
@@ -73,6 +75,11 @@ static void secondary_vm_do_failover(void)
     migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
                       MIGRATION_STATUS_COMPLETED);
 
+    replication_stop_all(true, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+    }
+
     if (!autostart) {
         error_report("\"-S\" qemu option will be ignored in secondary side");
         /* recover runstate to normal migration finish state */
@@ -110,6 +117,7 @@ static void primary_vm_do_failover(void)
 {
     MigrationState *s = migrate_get_current();
     int old_state;
+    Error *local_err = NULL;
 
     migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
                       MIGRATION_STATUS_COMPLETED);
@@ -133,6 +141,13 @@ static void primary_vm_do_failover(void)
                      FailoverStatus_str(old_state));
         return;
     }
+
+    replication_stop_all(true, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+        local_err = NULL;
+    }
+
     /* Notify COLO thread that failover work is finished */
     qemu_sem_post(&s->colo_exit_sem);
 }
@@ -356,6 +371,11 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
     qemu_savevm_state_header(fb);
     qemu_savevm_state_setup(fb);
     qemu_mutex_lock_iothread();
+    replication_do_checkpoint_all(&local_err);
+    if (local_err) {
+        qemu_mutex_unlock_iothread();
+        goto out;
+    }
     qemu_savevm_state_complete_precopy(fb, false, false);
     qemu_mutex_unlock_iothread();
 
@@ -446,6 +466,12 @@ static void colo_process_checkpoint(MigrationState *s)
     object_unref(OBJECT(bioc));
 
     qemu_mutex_lock_iothread();
+    replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
+    if (local_err) {
+        qemu_mutex_unlock_iothread();
+        goto out;
+    }
+
     vm_start();
     qemu_mutex_unlock_iothread();
     trace_colo_vm_state_change("stop", "run");
@@ -586,6 +612,11 @@ void *colo_process_incoming_thread(void *opaque)
     object_unref(OBJECT(bioc));
 
     qemu_mutex_lock_iothread();
+    replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
+    if (local_err) {
+        qemu_mutex_unlock_iothread();
+        goto out;
+    }
     vm_start();
     trace_colo_vm_state_change("stop", "run");
     qemu_mutex_unlock_iothread();
@@ -666,6 +697,18 @@ void *colo_process_incoming_thread(void *opaque)
             goto out;
         }
 
+        replication_get_error_all(&local_err);
+        if (local_err) {
+            qemu_mutex_unlock_iothread();
+            goto out;
+        }
+        /* discard colo disk buffer */
+        replication_do_checkpoint_all(&local_err);
+        if (local_err) {
+            qemu_mutex_unlock_iothread();
+            goto out;
+        }
+
         vmstate_loading = false;
         vm_start();
         trace_colo_vm_state_change("stop", "run");
migration/migration.c +10 -0
index 2f3e9d40d1..f4ab7b2d01 100644
@@ -386,6 +386,7 @@ static void process_incoming_migration_co(void *opaque)
     MigrationIncomingState *mis = migration_incoming_get_current();
     PostcopyState ps;
     int ret;
+    Error *local_err = NULL;
 
     assert(mis->from_src_file);
     mis->migration_incoming_co = qemu_coroutine_self();
@@ -418,6 +419,15 @@ static void process_incoming_migration_co(void *opaque)
 
     /* we get COLO info, and know if we are in COLO mode */
     if (!ret && migration_incoming_enable_colo()) {
+        /* Make sure all file formats flush their mutable metadata */
+        bdrv_invalidate_cache_all(&local_err);
+        if (local_err) {
+            migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
+                    MIGRATION_STATUS_FAILED);
+            error_report_err(local_err);
+            exit(EXIT_FAILURE);
+        }
+
         qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
              colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
         mis->have_colo_incoming_thread = true;
Signature · 1 line
2.17.GIT
author Reviewed-by Dr. David Alan Gilbert on v12 5/19
#8 lore raw

Re: [Qemu-devel] [PATCH V12 05/19] COLO: Add block replication into colo process

Sorry, forgot to add the reviewed by:

Thanks
Zhang Chen

Quoted 160 lines · Zhang Chen

Make sure master start block replication after slave's block replication started.

Besides, we need to activate VM's blocks before goes into COLO state.

---
 migration/colo.c      | 43 +++++++++++++++++++++++++++++++++++++++++++
 migration/migration.c | 10 ++++++++++
 2 files changed, 53 insertions(+)
migration/colo.c +7 -0
index f4bdfde170..af04010061 100644
@@ -27,6 +27,7 @@
 #include "replication.h"
 #include "net/colo-compare.h"
 #include "net/colo.h"
+#include "block/block.h"
 
 static bool vmstate_loading;
 static Notifier packets_compare_notifier;
@@ -56,6 +57,7 @@ static void secondary_vm_do_failover(void)
 {
     int old_state;
     MigrationIncomingState *mis = migration_incoming_get_current();
+    Error *local_err = NULL;
 
     /* Can not do failover during the process of VM's loading VMstate, Or
      * it will break the secondary VM.
@@ -73,6 +75,11 @@ static void secondary_vm_do_failover(void)
     migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
                       MIGRATION_STATUS_COMPLETED);
 
+    replication_stop_all(true, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+    }
+
     if (!autostart) {
         error_report("\"-S\" qemu option will be ignored in secondary
 side");
         /* recover runstate to normal migration finish state */
migration/colo.c +36 -0
@@ -110,6 +117,7 @@ static void primary_vm_do_failover(void)
 {
     MigrationState *s = migrate_get_current();
     int old_state;
+    Error *local_err = NULL;
 
     migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
                       MIGRATION_STATUS_COMPLETED);
@@ -133,6 +141,13 @@ static void primary_vm_do_failover(void)
                      FailoverStatus_str(old_state));
         return;
     }
+
+    replication_stop_all(true, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+        local_err = NULL;
+    }
+
     /* Notify COLO thread that failover work is finished */
     qemu_sem_post(&s->colo_exit_sem);
 }
@@ -356,6 +371,11 @@ static int
 colo_do_checkpoint_transaction(MigrationState *s,
     qemu_savevm_state_header(fb);
     qemu_savevm_state_setup(fb);
     qemu_mutex_lock_iothread();
+    replication_do_checkpoint_all(&local_err);
+    if (local_err) {
+        qemu_mutex_unlock_iothread();
+        goto out;
+    }
     qemu_savevm_state_complete_precopy(fb, false, false);
     qemu_mutex_unlock_iothread();
@@ -446,6 +466,12 @@ static void colo_process_checkpoint(MigrationState *s)
     object_unref(OBJECT(bioc));
 
     qemu_mutex_lock_iothread();
+    replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
+    if (local_err) {
+        qemu_mutex_unlock_iothread();
+        goto out;
+    }
+
     vm_start();
     qemu_mutex_unlock_iothread();
     trace_colo_vm_state_change("stop", "run");
@@ -586,6 +612,11 @@ void *colo_process_incoming_thread(void *opaque)
     object_unref(OBJECT(bioc));
 
     qemu_mutex_lock_iothread();
+    replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
+    if (local_err) {
+        qemu_mutex_unlock_iothread();
+        goto out;
+    }
     vm_start();
     trace_colo_vm_state_change("stop", "run");
     qemu_mutex_unlock_iothread();
@@ -666,6 +697,18 @@ void *colo_process_incoming_thread(void *opaque)
             goto out;
         }
 
+        replication_get_error_all(&local_err);
+        if (local_err) {
+            qemu_mutex_unlock_iothread();
+            goto out;
+        }
+        /* discard colo disk buffer */
+        replication_do_checkpoint_all(&local_err);
+        if (local_err) {
+            qemu_mutex_unlock_iothread();
+            goto out;
+        }
+
         vmstate_loading = false;
         vm_start();
         trace_colo_vm_state_change("stop", "run");
migration/migration.c +10 -0
index 2f3e9d40d1..f4ab7b2d01 100644
@@ -386,6 +386,7 @@ static void process_incoming_migration_co(void *opaque)
     MigrationIncomingState *mis = migration_incoming_get_current();
     PostcopyState ps;
     int ret;
+    Error *local_err = NULL;
 
     assert(mis->from_src_file);
     mis->migration_incoming_co = qemu_coroutine_self();
@@ -418,6 +419,15 @@ static void process_incoming_migration_co(void
 *opaque)
 
     /* we get COLO info, and know if we are in COLO mode */
     if (!ret && migration_incoming_enable_colo()) {
+        /* Make sure all file formats flush their mutable metadata */
+        bdrv_invalidate_cache_all(&local_err);
+        if (local_err) {
+            migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
+                    MIGRATION_STATUS_FAILED);
+            error_report_err(local_err);
+            exit(EXIT_FAILURE);
+        }
+
         qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
              colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
         mis->have_colo_incoming_thread = true;

--
2.17.GIT

#9 lore raw

Re: [Qemu-devel] [PATCH V12 05/19] COLO: Add block replication into colo process

In reply to Zhang Chen
Quoted 10 lines · Zhang Chen

Make sure master start block replication after slave's block replication started.

Besides, we need to activate VM's blocks before goes into COLO state.

Hi,

one of the recent merged PULL-requests broke compilation with
--disable-replication:

https://gitlab.com/huth/qemu/-/jobs/111968709

Seems like it happened due to this patch here. Could you please have a look?

Thanks,
Thomas

author
#10 lore raw

Re: [Qemu-devel] [PATCH V12 05/19] COLO: Add block replication into colo process

Quoted 14 lines · Thomas Huth
Quoted 10 lines · Zhang Chen

Make sure master start block replication after slave's block replication started.

Besides, we need to activate VM's blocks before goes into COLO state.

Hi,

Quoted 8 lines

one of the recent merged PULL-requests broke compilation with
--disable-replication:

https://gitlab.com/huth/qemu/-/jobs/111968709

Seems like it happened due to this patch here. Could you please have a look?

Sure, I will send a patch to fix this issue.

Thanks
Zhang Chen

Quoted 4 lines

Thanks,
Thomas

author
#11 lore raw

PATCH v12 6/19 COLO: Remove colo_state migration struct

In reply to Zhang Chen

We need to know if migration is going into COLO state for incoming side before start normal migration.

Instead by using the VMStateDescription to send colo_state from source side to destination side, we use MIG_CMD_ENABLE_COLO to indicate whether COLO is enabled or not.

---
 include/migration/colo.h |  5 +--
 migration/Makefile.objs  |  2 +-
 migration/colo-comm.c    | 76 ----------------------------------------
 migration/colo.c         | 13 ++++++-
 migration/migration.c    | 23 +++++++++++-
 migration/savevm.c       | 17 +++++++++
 migration/savevm.h       |  1 +
 migration/trace-events   |  1 +
 vl.c                     |  2 --
 9 files changed, 57 insertions(+), 83 deletions(-)
 delete mode 100644 migration/colo-comm.c
include/migration/colo.h +3 -2
index fefb2fcf4c..99ce17aca7 100644
@@ -28,8 +28,9 @@ void migrate_start_colo_process(MigrationState *s);
 bool migration_in_colo_state(void);
 
 /* loadvm */
-bool migration_incoming_enable_colo(void);
-void migration_incoming_exit_colo(void);
+void migration_incoming_enable_colo(void);
+void migration_incoming_disable_colo(void);
+bool migration_incoming_colo_enabled(void);
 void *colo_process_incoming_thread(void *opaque);
 bool migration_incoming_in_colo_state(void);
 
migration/Makefile.objs +1 -1
index c83ec47ba8..a4f3bafd86 100644
@@ -1,6 +1,6 @@
 common-obj-y += migration.o socket.o fd.o exec.o
 common-obj-y += tls.o channel.o savevm.o
-common-obj-y += colo-comm.o colo.o colo-failover.o
+common-obj-y += colo.o colo-failover.o
 common-obj-y += vmstate.o vmstate-types.o page_cache.o
 common-obj-y += qemu-file.o global_state.o
 common-obj-y += qemu-file-channel.o
migration/colo-comm.c deleted +0 -76

76 lines not on this page · raw message

migration/colo.c +12 -1
index af04010061..d3163b51c8 100644
@@ -152,6 +152,17 @@ static void primary_vm_do_failover(void)
     qemu_sem_post(&s->colo_exit_sem);
 }
 
+COLOMode get_colo_mode(void)
+{
+    if (migration_in_colo_state()) {
+        return COLO_MODE_PRIMARY;
+    } else if (migration_incoming_in_colo_state()) {
+        return COLO_MODE_SECONDARY;
+    } else {
+        return COLO_MODE_UNKNOWN;
+    }
+}
+
 void colo_do_failover(MigrationState *s)
 {
     /* Make sure VM stopped while failover happened. */
@@ -746,7 +757,7 @@ out:
     if (mis->to_src_file) {
         qemu_fclose(mis->to_src_file);
     }
-    migration_incoming_exit_colo();
+    migration_incoming_disable_colo();
 
     rcu_unregister_thread();
     return NULL;
migration/migration.c +22 -1
index f4ab7b2d01..950b22df63 100644
@@ -296,6 +296,22 @@ int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
     return migrate_send_rp_message(mis, msg_type, msglen, bufc);
 }
 
+static bool migration_colo_enabled;
+bool migration_incoming_colo_enabled(void)
+{
+    return migration_colo_enabled;
+}
+
+void migration_incoming_disable_colo(void)
+{
+    migration_colo_enabled = false;
+}
+
+void migration_incoming_enable_colo(void)
+{
+    migration_colo_enabled = true;
+}
+
 void qemu_start_incoming_migration(const char *uri, Error **errp)
 {
     const char *p;
@@ -418,7 +434,7 @@ static void process_incoming_migration_co(void *opaque)
     }
 
     /* we get COLO info, and know if we are in COLO mode */
-    if (!ret && migration_incoming_enable_colo()) {
+    if (!ret && migration_incoming_colo_enabled()) {
         /* Make sure all file formats flush their mutable metadata */
         bdrv_invalidate_cache_all(&local_err);
         if (local_err) {
@@ -3010,6 +3026,11 @@ static void *migration_thread(void *opaque)
         qemu_savevm_send_postcopy_advise(s->to_dst_file);
     }
 
+    if (migrate_colo_enabled()) {
+        /* Notify migration destination that we enable COLO */
+        qemu_savevm_send_colo_enable(s->to_dst_file);
+    }
+
     qemu_savevm_state_setup(s->to_dst_file);
 
     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
migration/savevm.c +17 -0
index 13e51f0e34..0376e723b6 100644
@@ -56,6 +56,7 @@
 #include "io/channel-file.h"
 #include "sysemu/replay.h"
 #include "qjson.h"
+#include "migration/colo.h"
 
 #ifndef ETH_P_RARP
 #define ETH_P_RARP 0x8035
@@ -82,6 +83,7 @@ enum qemu_vm_cmd {
                                       were previously sent during
                                       precopy but are dirty. */
     MIG_CMD_PACKAGED,          /* Send a wrapped stream within this stream */
+    MIG_CMD_ENABLE_COLO,       /* Enable COLO */
     MIG_CMD_POSTCOPY_RESUME,   /* resume postcopy on dest */
     MIG_CMD_RECV_BITMAP,       /* Request for recved bitmap on dst */
     MIG_CMD_MAX
@@ -841,6 +843,12 @@ static void qemu_savevm_command_send(QEMUFile *f,
     qemu_fflush(f);
 }
 
+void qemu_savevm_send_colo_enable(QEMUFile *f)
+{
+    trace_savevm_send_colo_enable();
+    qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL);
+}
+
 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
 {
     uint32_t buf;
@@ -1921,6 +1929,12 @@ static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
     return 0;
 }
 
+static int loadvm_process_enable_colo(MigrationIncomingState *mis)
+{
+    migration_incoming_enable_colo();
+    return 0;
+}
+
 /*
  * Process an incoming 'QEMU_VM_COMMAND'
  * 0           just a normal return
@@ -2000,6 +2014,9 @@ static int loadvm_process_command(QEMUFile *f)
 
     case MIG_CMD_RECV_BITMAP:
         return loadvm_handle_recv_bitmap(mis, len);
+
+    case MIG_CMD_ENABLE_COLO:
+        return loadvm_process_enable_colo(mis);
     }
 
     return 0;
migration/savevm.h +1 -0
index a5e65b8ae3..8373c2f6bd 100644
@@ -55,6 +55,7 @@ void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
                                            uint16_t len,
                                            uint64_t *start_list,
                                            uint64_t *length_list);
+void qemu_savevm_send_colo_enable(QEMUFile *f);
 
 int qemu_loadvm_state(QEMUFile *f);
 void qemu_loadvm_state_cleanup(void);
migration/trace-events +1 -0
index 9430f3cbe0..fa0ff3f3bf 100644
@@ -37,6 +37,7 @@ savevm_send_ping(uint32_t val) "0x%x"
 savevm_send_postcopy_listen(void) ""
 savevm_send_postcopy_run(void) ""
 savevm_send_postcopy_resume(void) ""
+savevm_send_colo_enable(void) ""
 savevm_send_recv_bitmap(char *name) "%s"
 savevm_state_setup(void) ""
 savevm_state_resume_prepare(void) ""
vl.c +0 -2
index 5ba06adf78..539e90e133 100644
@@ -4396,8 +4396,6 @@ int main(int argc, char **argv, char **envp)
 #endif
     }
 
-    colo_info_init();
-
     if (net_init_clients(&err) < 0) {
         error_report_err(err);
         exit(1);
Signature · 1 line
2.17.GIT
author
#12 lore raw

PATCH v12 7/19 COLO: Load dirty pages into SVM's RAM cache firstly

In reply to Zhang Chen

We should not load PVM's state directly into SVM, because there maybe some errors happen when SVM is receving data, which will break SVM.

We need to ensure receving all data before load the state into SVM. We use an extra memory to cache these data (PVM's ram). The ram cache in secondary side is initially the same as SVM/PVM's memory. And in the process of checkpoint, we cache the dirty pages of PVM into this ram cache firstly, so this ram cache always the same as PVM's memory at every checkpoint, then we flush this cached ram to SVM after we receive all PVM's state.

---
 include/exec/ram_addr.h |  1 +
 migration/migration.c   |  7 ++++
 migration/ram.c         | 83 ++++++++++++++++++++++++++++++++++++++++-
 migration/ram.h         |  4 ++
 migration/savevm.c      |  2 +-
 5 files changed, 94 insertions(+), 3 deletions(-)
include/exec/ram_addr.h +1 -0
index 3abb639056..9ecd911c3e 100644
@@ -27,6 +27,7 @@ struct RAMBlock {
     struct rcu_head rcu;
     struct MemoryRegion *mr;
     uint8_t *host;
+    uint8_t *colo_cache; /* For colo, VM's ram cache */
     ram_addr_t offset;
     ram_addr_t used_length;
     ram_addr_t max_length;
migration/migration.c +7 -0
index 950b22df63..d67fcff359 100644
@@ -444,6 +444,11 @@ static void process_incoming_migration_co(void *opaque)
             exit(EXIT_FAILURE);
         }
 
+        if (colo_init_ram_cache() < 0) {
+            error_report("Init ram cache failed");
+            exit(EXIT_FAILURE);
+        }
+
         qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
              colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
         mis->have_colo_incoming_thread = true;
@@ -451,6 +456,8 @@ static void process_incoming_migration_co(void *opaque)
 
         /* Wait checkpoint incoming thread exit before free resource */
         qemu_thread_join(&mis->colo_incoming_thread);
+        /* We hold the global iothread lock, so it is safe here */
+        colo_release_ram_cache();
     }
 
     if (ret < 0) {
migration/ram.c +81 -2

118 lines not on this page · raw message

migration/ram.h +4 -0
index 457bf54b8c..d009480494 100644
@@ -70,4 +70,8 @@ int64_t ramblock_recv_bitmap_send(QEMUFile *file,
                                   const char *block_name);
 int ram_dirty_bitmap_reload(MigrationState *s, RAMBlock *rb);
 
+/* ram cache */
+int colo_init_ram_cache(void);
+void colo_release_ram_cache(void);
+
 #endif
migration/savevm.c +1 -1
index 0376e723b6..96db539064 100644
@@ -1932,7 +1932,7 @@ static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
 static int loadvm_process_enable_colo(MigrationIncomingState *mis)
 {
     migration_incoming_enable_colo();
-    return 0;
+    return colo_init_ram_cache();
 }
 
 /*
Signature · 1 line
2.17.GIT
author
#13 lore raw

PATCH v12 8/19 ram/COLO: Record the dirty pages that SVM received

In reply to Zhang Chen

We record the address of the dirty pages that received, it will help flushing pages that cached into SVM.

Here, it is a trick, we record dirty pages by re-using migration dirty bitmap. In the later patch, we will start the dirty log for SVM, just like migration, in this way, we can record both the dirty pages caused by PVM and SVM, we only flush those dirty pages from RAM cache while do checkpoint.

---
 migration/ram.c | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)
migration/ram.c +40 -3
index a63315533e..df8202216f 100644
@@ -3409,6 +3409,15 @@ static inline void *colo_cache_from_block_offset(RAMBlock *block,
                      __func__, block->idstr);
         return NULL;
     }
+
+    /*
+    * During colo checkpoint, we need bitmap of these migrated pages.
+    * It help us to decide which pages in ram cache should be flushed
+    * into VM's RAM later.
+    */
+    if (!test_and_set_bit(offset >> TARGET_PAGE_BITS, block->bmap)) {
+        ram_state->migration_dirty_pages++;
+    }
     return block->colo_cache + offset;
 }
 
@@ -3626,7 +3635,7 @@ int colo_init_ram_cache(void)
     RAMBlock *block;
 
     rcu_read_lock();
-    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
         block->colo_cache = qemu_anon_ram_alloc(block->used_length,
                                                 NULL,
                                                 false);
@@ -3639,10 +3648,29 @@ int colo_init_ram_cache(void)
         memcpy(block->colo_cache, block->host, block->used_length);
     }
     rcu_read_unlock();
+    /*
+    * Record the dirty pages that sent by PVM, we use this dirty bitmap together
+    * with to decide which page in cache should be flushed into SVM's RAM. Here
+    * we use the same name 'ram_bitmap' as for migration.
+    */
+    if (ram_bytes_total()) {
+        RAMBlock *block;
+
+        RAMBLOCK_FOREACH_MIGRATABLE(block) {
+            unsigned long pages = block->max_length >> TARGET_PAGE_BITS;
+
+            block->bmap = bitmap_new(pages);
+            bitmap_set(block->bmap, 0, pages);
+        }
+    }
+    ram_state = g_new0(RAMState, 1);
+    ram_state->migration_dirty_pages = 0;
+
     return 0;
 
 out_locked:
-    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
+
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
         if (block->colo_cache) {
             qemu_anon_ram_free(block->colo_cache, block->used_length);
             block->colo_cache = NULL;
@@ -3658,14 +3686,23 @@ void colo_release_ram_cache(void)
 {
     RAMBlock *block;
 
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        g_free(block->bmap);
+        block->bmap = NULL;
+    }
+
     rcu_read_lock();
-    QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
+
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
         if (block->colo_cache) {
             qemu_anon_ram_free(block->colo_cache, block->used_length);
             block->colo_cache = NULL;
         }
     }
+
     rcu_read_unlock();
+    g_free(ram_state);
+    ram_state = NULL;
 }
 
 /**
Signature · 1 line
2.17.GIT
author
#14 lore raw

PATCH v12 9/19 COLO: Flush memory data from ram cache

In reply to Zhang Chen

During the time of VM's running, PVM may dirty some pages, we will transfer PVM's dirty pages to SVM and store them into SVM's RAM cache at next checkpoint time. So, the content of SVM's RAM cache will always be same with PVM's memory after checkpoint.

Instead of flushing all content of PVM's RAM cache into SVM's MEMORY, we do this in a more efficient way:
Only flush any page that dirtied by PVM since last checkpoint. In this way, we can ensure SVM's memory same with PVM's.

Besides, we must ensure flush RAM cache before load device state.

---
 migration/ram.c        | 37 +++++++++++++++++++++++++++++++++++++
 migration/trace-events |  2 ++
 2 files changed, 39 insertions(+)
migration/ram.c +37 -0
index df8202216f..a478d85740 100644
@@ -3925,6 +3925,39 @@ static bool postcopy_is_running(void)
     return ps >= POSTCOPY_INCOMING_LISTENING && ps < POSTCOPY_INCOMING_END;
 }
 
+/*
+ * Flush content of RAM cache into SVM's memory.
+ * Only flush the pages that be dirtied by PVM or SVM or both.
+ */
+static void colo_flush_ram_cache(void)
+{
+    RAMBlock *block = NULL;
+    void *dst_host;
+    void *src_host;
+    unsigned long offset = 0;
+
+    trace_colo_flush_ram_cache_begin(ram_state->migration_dirty_pages);
+    rcu_read_lock();
+    block = QLIST_FIRST_RCU(&ram_list.blocks);
+
+    while (block) {
+        offset = migration_bitmap_find_dirty(ram_state, block, offset);
+
+        if (offset << TARGET_PAGE_BITS >= block->used_length) {
+            offset = 0;
+            block = QLIST_NEXT_RCU(block, next);
+        } else {
+            migration_bitmap_clear_dirty(ram_state, block, offset);
+            dst_host = block->host + (offset << TARGET_PAGE_BITS);
+            src_host = block->colo_cache + (offset << TARGET_PAGE_BITS);
+            memcpy(dst_host, src_host, TARGET_PAGE_SIZE);
+        }
+    }
+
+    rcu_read_unlock();
+    trace_colo_flush_ram_cache_end();
+}
+
 static int ram_load(QEMUFile *f, void *opaque, int version_id)
 {
     int flags = 0, ret = 0, invalid_flags = 0;
@@ -4101,6 +4134,10 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
     ret |= wait_for_decompress_done();
     rcu_read_unlock();
     trace_ram_load_complete(ret, seq_iter);
+
+    if (!ret  && migration_incoming_in_colo_state()) {
+        colo_flush_ram_cache();
+    }
     return ret;
 }
 
migration/trace-events +2 -0
index fa0ff3f3bf..bd2d0cd25a 100644
@@ -102,6 +102,8 @@ ram_dirty_bitmap_sync_start(void) ""
 ram_dirty_bitmap_sync_wait(void) ""
 ram_dirty_bitmap_sync_complete(void) ""
 ram_state_resume_prepare(uint64_t v) "%" PRId64
+colo_flush_ram_cache_begin(uint64_t dirty_pages) "dirty_pages %" PRIu64
+colo_flush_ram_cache_end(void) ""
 
 # migration/migration.c
 await_return_path_close_on_source_close(void) ""
Signature · 1 line
2.17.GIT
author
#15 lore raw

PATCH v12 10/19 qmp event: Add COLO_EXIT event to notify users while exited COLO

In reply to Zhang Chen

From: zhanghailiang <[email protected]>

If some errors happen during VM's COLO FT stage, it's important to notify the users of this event. Together with 'x-colo-lost-heartbeat', Users can intervene in COLO's failover work immediately.
If users don't want to get involved in COLO's failover verdict, it is still necessary to notify users that we exited COLO mode.

---
 migration/colo.c    | 31 +++++++++++++++++++++++++++++++
 qapi/migration.json | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
migration/colo.c +31 -0
index d3163b51c8..761fbb9f78 100644
@@ -28,6 +28,7 @@
 #include "net/colo-compare.h"
 #include "net/colo.h"
 #include "block/block.h"
+#include "qapi/qapi-events-migration.h"
 
 static bool vmstate_loading;
 static Notifier packets_compare_notifier;
@@ -514,6 +515,23 @@ out:
         qemu_fclose(fb);
     }
 
+    /*
+     * There are only two reasons we can get here, some error happened
+     * or the user triggered failover.
+     */
+    switch (failover_get_state()) {
+    case FAILOVER_STATUS_NONE:
+        qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
+                                  COLO_EXIT_REASON_ERROR, NULL);
+        break;
+    case FAILOVER_STATUS_REQUIRE:
+        qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
+                                  COLO_EXIT_REASON_REQUEST, NULL);
+        break;
+    default:
+        abort();
+    }
+
     /* Hope this not to be too long to wait here */
     qemu_sem_wait(&s->colo_exit_sem);
     qemu_sem_destroy(&s->colo_exit_sem);
@@ -746,6 +764,19 @@ out:
         error_report_err(local_err);
     }
 
+    switch (failover_get_state()) {
+    case FAILOVER_STATUS_NONE:
+        qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
+                                  COLO_EXIT_REASON_ERROR, NULL);
+        break;
+    case FAILOVER_STATUS_REQUIRE:
+        qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
+                                  COLO_EXIT_REASON_REQUEST, NULL);
+        break;
+    default:
+        abort();
+    }
+
     if (fb) {
         qemu_fclose(fb);
     }
qapi/migration.json +38 -0
index f62d3f9a4b..9b3b1c0c4e 100644
@@ -932,6 +932,44 @@
 { 'enum': 'FailoverStatus',
   'data': [ 'none', 'require', 'active', 'completed', 'relaunch' ] }
 
+##
+# @COLO_EXIT:
+#
+# Emitted when VM finishes COLO mode due to some errors happening or
+# at the request of users.
+#
+# @mode: report COLO mode when COLO exited.
+#
+# @reason: describes the reason for the COLO exit.
+#
+# Since: 3.1
+#
+# Example:
+#
+# <- { "timestamp": {"seconds": 2032141960, "microseconds": 417172},
+#      "event": "COLO_EXIT", "data": {"mode": "primary", "reason": "request" } }
+#
+##
+{ 'event': 'COLO_EXIT',
+  'data': {'mode': 'COLOMode', 'reason': 'COLOExitReason' } }
+
+##
+# @COLOExitReason:
+#
+# The reason for a COLO exit
+#
+# @none: no failover has ever happened. This can't occur in the
+# COLO_EXIT event, only in the result of query-colo-status.
+#
+# @request: COLO exit is due to an external request
+#
+# @error: COLO exit is due to an internal error
+#
+# Since: 3.1
+##
+{ 'enum': 'COLOExitReason',
+  'data': [ 'none', 'request', 'error' ] }
+
 ##
 # @x-colo-lost-heartbeat:
 #
Signature · 1 line
2.17.GIT
author
#16 lore raw

PATCH v12 11/19 qapi/migration.json: Rename COLO unknown mode to none mode.

In reply to Zhang Chen

From: Zhang Chen <[email protected]>

Suggested by Markus Armbruster rename COLO unknown mode to none mode.

---
 migration/colo-failover.c |  2 +-
 migration/colo.c          |  2 +-
 qapi/migration.json       | 10 +++++-----
 3 files changed, 7 insertions(+), 7 deletions(-)
migration/colo-failover.c +1 -1
index 0ae0c41221..4854a96c92 100644
@@ -77,7 +77,7 @@ FailoverStatus failover_get_state(void)
 
 void qmp_x_colo_lost_heartbeat(Error **errp)
 {
-    if (get_colo_mode() == COLO_MODE_UNKNOWN) {
+    if (get_colo_mode() == COLO_MODE_NONE) {
         error_setg(errp, QERR_FEATURE_DISABLED, "colo");
         return;
     }
migration/colo.c +1 -1
index 761fbb9f78..9107ac124a 100644
@@ -160,7 +160,7 @@ COLOMode get_colo_mode(void)
     } else if (migration_incoming_in_colo_state()) {
         return COLO_MODE_SECONDARY;
     } else {
-        return COLO_MODE_UNKNOWN;
+        return COLO_MODE_NONE;
     }
 }
 
qapi/migration.json +5 -5
index 9b3b1c0c4e..c0bc77fd8d 100644
@@ -899,18 +899,18 @@
 ##
 # @COLOMode:
 #
-# The colo mode
+# The COLO current mode.
 #
-# @unknown: unknown mode
+# @none: COLO is disabled.
 #
-# @primary: master side
+# @primary: COLO node in primary side.
 #
-# @secondary: slave side
+# @secondary: COLO node in slave side.
 #
 # Since: 2.8
 ##
 { 'enum': 'COLOMode',
-  'data': [ 'unknown', 'primary', 'secondary'] }
+  'data': [ 'none', 'primary', 'secondary'] }
 
 ##
 # @FailoverStatus:
Signature · 1 line
2.17.GIT
author
#17 lore raw

PATCH v12 12/19 qapi: Add new command to query colo status

In reply to Zhang Chen

Libvirt or other high level software can use this command query colo status. You can test this command like that:
{'execute':'query-colo-status'}

---
 migration/colo.c    | 21 +++++++++++++++++++++
 qapi/migration.json | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
migration/colo.c +21 -0
index 9107ac124a..8c4ba373aa 100644
@@ -29,6 +29,7 @@
 #include "net/colo.h"
 #include "block/block.h"
 #include "qapi/qapi-events-migration.h"
+#include "qapi/qmp/qerror.h"
 
 static bool vmstate_loading;
 static Notifier packets_compare_notifier;
@@ -237,6 +238,26 @@ void qmp_xen_colo_do_checkpoint(Error **errp)
 #endif
 }
 
+COLOStatus *qmp_query_colo_status(Error **errp)
+{
+    COLOStatus *s = g_new0(COLOStatus, 1);
+
+    s->mode = get_colo_mode();
+
+    switch (failover_get_state()) {
+    case FAILOVER_STATUS_NONE:
+        s->reason = COLO_EXIT_REASON_NONE;
+        break;
+    case FAILOVER_STATUS_REQUIRE:
+        s->reason = COLO_EXIT_REASON_REQUEST;
+        break;
+    default:
+        s->reason = COLO_EXIT_REASON_ERROR;
+    }
+
+    return s;
+}
+
 static void colo_send_message(QEMUFile *f, COLOMessage msg,
                               Error **errp)
 {
qapi/migration.json +32 -0
index c0bc77fd8d..840a118d11 100644
@@ -1283,6 +1283,38 @@
 ##
 { 'command': 'xen-colo-do-checkpoint' }
 
+##
+# @COLOStatus:
+#
+# The result format for 'query-colo-status'.
+#
+# @mode: COLO running mode. If COLO is running, this field will return
+#        'primary' or 'secondary'.
+#
+# @reason: describes the reason for the COLO exit.
+#
+# Since: 3.0
+##
+{ 'struct': 'COLOStatus',
+  'data': { 'mode': 'COLOMode', 'reason': 'COLOExitReason' } }
+
+##
+# @query-colo-status:
+#
+# Query COLO status while the vm is running.
+#
+# Returns: A @COLOStatus object showing the status.
+#
+# Example:
+#
+# -> { "execute": "query-colo-status" }
+# <- { "return": { "mode": "primary", "active": true, "reason": "request" } }
+#
+# Since: 3.0
+##
+{ 'command': 'query-colo-status',
+  'returns': 'COLOStatus' }
+
 ##
 # @migrate-recover:
 #
Signature · 1 line
2.17.GIT
author
#18 lore raw

PATCH v12 13/19 savevm: split the process of different stages for loadvm/savevm

In reply to Zhang Chen

There are several stages during loadvm/savevm process. In different stage, migration incoming processes different types of sections.
We want to control these stages more accuracy, it will benefit COLO performance, we don't have to save type of QEMU_VM_SECTION_START sections everytime while do checkpoint, besides, we want to separate the process of saving/loading memory and devices state.

So we add three new helper functions: qemu_load_device_state() and qemu_savevm_live_state() to achieve different process during migration.

Besides, we make qemu_loadvm_state_main() and qemu_save_device_state() public, and simplify the codes of qemu_save_device_state() by calling the wrapper qemu_savevm_state_header().

---
 migration/colo.c   | 41 ++++++++++++++++++++++++++++++++---------
 migration/savevm.c | 36 +++++++++++++++++++++++++++++-------
 migration/savevm.h |  4 ++++
 3 files changed, 65 insertions(+), 16 deletions(-)
migration/colo.c +32 -9
index 8c4ba373aa..25d279decf 100644
@@ -30,6 +30,7 @@
 #include "block/block.h"
 #include "qapi/qapi-events-migration.h"
 #include "qapi/qmp/qerror.h"
+#include "sysemu/cpus.h"
 
 static bool vmstate_loading;
 static Notifier packets_compare_notifier;
@@ -401,23 +402,34 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
 
     /* Disable block migration */
     migrate_set_block_enabled(false, &local_err);
-    qemu_savevm_state_header(fb);
-    qemu_savevm_state_setup(fb);
     qemu_mutex_lock_iothread();
     replication_do_checkpoint_all(&local_err);
     if (local_err) {
         qemu_mutex_unlock_iothread();
         goto out;
     }
-    qemu_savevm_state_complete_precopy(fb, false, false);
-    qemu_mutex_unlock_iothread();
-
-    qemu_fflush(fb);
 
     colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
     if (local_err) {
+        qemu_mutex_unlock_iothread();
+        goto out;
+    }
+    /* Note: device state is saved into buffer */
+    ret = qemu_save_device_state(fb);
+
+    qemu_mutex_unlock_iothread();
+    if (ret < 0) {
         goto out;
     }
+    /*
+     * Only save VM's live state, which not including device state.
+     * TODO: We may need a timeout mechanism to prevent COLO process
+     * to be blocked here.
+     */
+    qemu_savevm_live_state(s->to_dst_file);
+
+    qemu_fflush(fb);
+
     /*
      * We need the size of the VMstate data in Secondary side,
      * With which we can decide how much data should be read.
@@ -635,6 +647,7 @@ void *colo_process_incoming_thread(void *opaque)
     uint64_t total_size;
     uint64_t value;
     Error *local_err = NULL;
+    int ret;
 
     rcu_register_thread();
     qemu_sem_init(&mis->colo_incoming_sem, 0);
@@ -708,6 +721,16 @@ void *colo_process_incoming_thread(void *opaque)
             goto out;
         }
 
+        qemu_mutex_lock_iothread();
+        cpu_synchronize_all_pre_loadvm();
+        ret = qemu_loadvm_state_main(mis->from_src_file, mis);
+        qemu_mutex_unlock_iothread();
+
+        if (ret < 0) {
+            error_report("Load VM's live state (ram) error");
+            goto out;
+        }
+
         value = colo_receive_message_value(mis->from_src_file,
                                  COLO_MESSAGE_VMSTATE_SIZE, &local_err);
         if (local_err) {
@@ -739,10 +762,10 @@ void *colo_process_incoming_thread(void *opaque)
         }
 
         qemu_mutex_lock_iothread();
-        qemu_system_reset(SHUTDOWN_CAUSE_NONE);
         vmstate_loading = true;
-        if (qemu_loadvm_state(fb) < 0) {
-            error_report("COLO: loadvm failed");
+        ret = qemu_load_device_state(fb);
+        if (ret < 0) {
+            error_report("COLO: load device state failed");
             qemu_mutex_unlock_iothread();
             goto out;
         }
migration/savevm.c +29 -7
index 96db539064..5636cbeae2 100644
@@ -1378,13 +1378,21 @@ done:
     return ret;
 }
 
-static int qemu_save_device_state(QEMUFile *f)
+void qemu_savevm_live_state(QEMUFile *f)
 {
-    SaveStateEntry *se;
+    /* save QEMU_VM_SECTION_END section */
+    qemu_savevm_state_complete_precopy(f, true, false);
+    qemu_put_byte(f, QEMU_VM_EOF);
+}
 
-    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
-    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
+int qemu_save_device_state(QEMUFile *f)
+{
+    SaveStateEntry *se;
 
+    if (!migration_in_colo_state()) {
+        qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
+        qemu_put_be32(f, QEMU_VM_FILE_VERSION);
+    }
     cpu_synchronize_all_states();
 
     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
@@ -1440,8 +1448,6 @@ enum LoadVMExitCodes {
     LOADVM_QUIT     =  1,
 };
 
-static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis);
-
 /* ------ incoming postcopy messages ------ */
 /* 'advise' arrives before any transfers just to tell us that a postcopy
  * *might* happen - it might be skipped if precopy transferred everything
@@ -2244,7 +2250,7 @@ static bool postcopy_pause_incoming(MigrationIncomingState *mis)
     return true;
 }
 
-static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
+int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
 {
     uint8_t section_type;
     int ret = 0;
@@ -2413,6 +2419,22 @@ int qemu_loadvm_state(QEMUFile *f)
     return ret;
 }
 
+int qemu_load_device_state(QEMUFile *f)
+{
+    MigrationIncomingState *mis = migration_incoming_get_current();
+    int ret;
+
+    /* Load QEMU_VM_SECTION_FULL section */
+    ret = qemu_loadvm_state_main(f, mis);
+    if (ret < 0) {
+        error_report("Failed to load device state: %d", ret);
+        return ret;
+    }
+
+    cpu_synchronize_all_post_init();
+    return 0;
+}
+
 int save_snapshot(const char *name, Error **errp)
 {
     BlockDriverState *bs, *bs1;
migration/savevm.h +4 -0
index 8373c2f6bd..51a4b9caa8 100644
@@ -56,8 +56,12 @@ void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
                                            uint64_t *start_list,
                                            uint64_t *length_list);
 void qemu_savevm_send_colo_enable(QEMUFile *f);
+void qemu_savevm_live_state(QEMUFile *f);
+int qemu_save_device_state(QEMUFile *f);
 
 int qemu_loadvm_state(QEMUFile *f);
 void qemu_loadvm_state_cleanup(void);
+int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis);
+int qemu_load_device_state(QEMUFile *f);
 
 #endif
Signature · 1 line
2.17.GIT
author
#19 lore raw

PATCH v12 14/19 COLO: flush host dirty ram from cache

In reply to Zhang Chen

From: zhanghailiang <[email protected]>

Don't need to flush all VM's ram from cache, only flush the dirty pages since last checkpoint

---
 migration/ram.c | 9 +++++++++
 1 file changed, 9 insertions(+)
migration/ram.c +9 -0
index a478d85740..739458be5b 100644
@@ -3665,6 +3665,7 @@ int colo_init_ram_cache(void)
     }
     ram_state = g_new0(RAMState, 1);
     ram_state->migration_dirty_pages = 0;
+    memory_global_dirty_log_start();
 
     return 0;
 
@@ -3686,6 +3687,7 @@ void colo_release_ram_cache(void)
 {
     RAMBlock *block;
 
+    memory_global_dirty_log_stop();
     RAMBLOCK_FOREACH_MIGRATABLE(block) {
         g_free(block->bmap);
         block->bmap = NULL;
@@ -3936,6 +3938,13 @@ static void colo_flush_ram_cache(void)
     void *src_host;
     unsigned long offset = 0;
 
+    memory_global_dirty_log_sync();
+    rcu_read_lock();
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        migration_bitmap_sync_range(ram_state, block, 0, block->used_length);
+    }
+    rcu_read_unlock();
+
     trace_colo_flush_ram_cache_begin(ram_state->migration_dirty_pages);
     rcu_read_lock();
     block = QLIST_FIRST_RCU(&ram_list.blocks);
Signature · 1 line
2.17.GIT
author
#20 lore raw

PATCH v12 15/19 filter: Add handle_event method for NetFilterClass

In reply to Zhang Chen

Filter needs to process the event of checkpoint/failover or other event passed by COLO frame.

---
 include/net/filter.h |  5 +++++
 net/filter.c         | 17 +++++++++++++++++
 net/net.c            | 19 +++++++++++++++++++
 3 files changed, 41 insertions(+)
include/net/filter.h +5 -0
index 435acd6f82..49da666ac0 100644
@@ -38,6 +38,8 @@ typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc,
 
 typedef void (FilterStatusChanged) (NetFilterState *nf, Error **errp);
 
+typedef void (FilterHandleEvent) (NetFilterState *nf, int event, Error **errp);
+
 typedef struct NetFilterClass {
     ObjectClass parent_class;
 
@@ -45,6 +47,7 @@ typedef struct NetFilterClass {
     FilterSetup *setup;
     FilterCleanup *cleanup;
     FilterStatusChanged *status_changed;
+    FilterHandleEvent *handle_event;
     /* mandatory */
     FilterReceiveIOV *receive_iov;
 } NetFilterClass;
@@ -77,4 +80,6 @@ ssize_t qemu_netfilter_pass_to_next(NetClientState *sender,
                                     int iovcnt,
                                     void *opaque);
 
+void colo_notify_filters_event(int event, Error **errp);
+
 #endif /* QEMU_NET_FILTER_H */
net/filter.c +17 -0
index 2fd7d7d663..c9f9e5fa08 100644
@@ -17,6 +17,8 @@
 #include "net/vhost_net.h"
 #include "qom/object_interfaces.h"
 #include "qemu/iov.h"
+#include "net/colo.h"
+#include "migration/colo.h"
 
 static inline bool qemu_can_skip_netfilter(NetFilterState *nf)
 {
@@ -245,11 +247,26 @@ static void netfilter_finalize(Object *obj)
     g_free(nf->netdev_id);
 }
 
+static void default_handle_event(NetFilterState *nf, int event, Error **errp)
+{
+    switch (event) {
+    case COLO_EVENT_CHECKPOINT:
+        break;
+    case COLO_EVENT_FAILOVER:
+        object_property_set_str(OBJECT(nf), "off", "status", errp);
+        break;
+    default:
+        break;
+    }
+}
+
 static void netfilter_class_init(ObjectClass *oc, void *data)
 {
     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+    NetFilterClass *nfc = NETFILTER_CLASS(oc);
 
     ucc->complete = netfilter_complete;
+    nfc->handle_event = default_handle_event;
 }
 
 static const TypeInfo netfilter_info = {
net/net.c +19 -0
index 2a3133990c..fd8efebfdb 100644
@@ -1331,6 +1331,25 @@ void hmp_info_network(Monitor *mon, const QDict *qdict)
     }
 }
 
+void colo_notify_filters_event(int event, Error **errp)
+{
+    NetClientState *nc;
+    NetFilterState *nf;
+    NetFilterClass *nfc = NULL;
+    Error *local_err = NULL;
+
+    QTAILQ_FOREACH(nc, &net_clients, next) {
+        QTAILQ_FOREACH(nf, &nc->filters, next) {
+            nfc = NETFILTER_GET_CLASS(OBJECT(nf));
+            nfc->handle_event(nf, event, &local_err);
+            if (local_err) {
+                error_propagate(errp, local_err);
+                return;
+            }
+        }
+    }
+}
+
 void qmp_set_link(const char *name, bool up, Error **errp)
 {
     NetClientState *ncs[MAX_QUEUE_NUM];
Signature · 1 line
2.17.GIT
author
#21 lore raw

PATCH v12 16/19 filter-rewriter: handle checkpoint and failover event

In reply to Zhang Chen

After one round of checkpoint, the states between PVM and SVM become consistent, so it is unnecessary to adjust the sequence of net packets for old connections, besides, while failover happens, filter-rewriter will into failover mode that needn't handle the new TCP connection.

---
 net/colo-compare.c    | 12 ++++-----
 net/colo.c            |  8 ++++++
 net/colo.h            |  2 ++
 net/filter-rewriter.c | 57 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 73 insertions(+), 6 deletions(-)
net/colo-compare.c +6 -6
index 426eab5973..a39191d522 100644
@@ -116,6 +116,12 @@ enum {
     SECONDARY_IN,
 };
 
+static void colo_compare_inconsistency_notify(void)
+{
+    notifier_list_notify(&colo_compare_notifiers,
+                migrate_get_current());
+}
+
 static int compare_chr_send(CompareState *s,
                             const uint8_t *buf,
                             uint32_t size,
@@ -562,12 +568,6 @@ static int colo_old_packet_check_one(Packet *pkt, int64_t *check_time)
     }
 }
 
-static void colo_compare_inconsistency_notify(void)
-{
-    notifier_list_notify(&colo_compare_notifiers,
-                migrate_get_current());
-}
-
 void colo_compare_register_notifier(Notifier *notify)
 {
     notifier_list_add(&colo_compare_notifiers, notify);
net/colo.c +8 -0
index 97c8fc928f..49176bf07b 100644
@@ -221,3 +221,11 @@ Connection *connection_get(GHashTable *connection_track_table,
 
     return conn;
 }
+
+bool connection_has_tracked(GHashTable *connection_track_table,
+                            ConnectionKey *key)
+{
+    Connection *conn = g_hash_table_lookup(connection_track_table, key);
+
+    return conn ? true : false;
+}
net/colo.h +2 -0
index 0277e0e9ba..11c5226488 100644
@@ -98,6 +98,8 @@ void connection_destroy(void *opaque);
 Connection *connection_get(GHashTable *connection_track_table,
                            ConnectionKey *key,
                            GQueue *conn_list);
+bool connection_has_tracked(GHashTable *connection_track_table,
+                            ConnectionKey *key);
 void connection_hashtable_reset(GHashTable *connection_track_table);
 Packet *packet_new(const void *data, int size, int vnet_hdr_len);
 void packet_destroy(void *opaque, void *user_data);
net/filter-rewriter.c +57 -0

105 lines not on this page · raw message

Signature · 1 line
2.17.GIT
author
#22 lore raw

PATCH v12 17/19 COLO: notify net filters about checkpoint/failover event

In reply to Zhang Chen

From: zhanghailiang <[email protected]>

Notify all net filters about the checkpoint and failover event.

---
 migration/colo.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
migration/colo.c +15 -0
index 25d279decf..365e913e51 100644
@@ -31,6 +31,7 @@
 #include "qapi/qapi-events-migration.h"
 #include "qapi/qmp/qerror.h"
 #include "sysemu/cpus.h"
+#include "net/filter.h"
 
 static bool vmstate_loading;
 static Notifier packets_compare_notifier;
@@ -83,6 +84,12 @@ static void secondary_vm_do_failover(void)
         error_report_err(local_err);
     }
 
+    /* Notify all filters of all NIC to do checkpoint */
+    colo_notify_filters_event(COLO_EVENT_FAILOVER, &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+    }
+
     if (!autostart) {
         error_report("\"-S\" qemu option will be ignored in secondary side");
         /* recover runstate to normal migration finish state */
@@ -782,6 +789,14 @@ void *colo_process_incoming_thread(void *opaque)
             goto out;
         }
 
+        /* Notify all filters of all NIC to do checkpoint */
+        colo_notify_filters_event(COLO_EVENT_CHECKPOINT, &local_err);
+
+        if (local_err) {
+            qemu_mutex_unlock_iothread();
+            goto out;
+        }
+
         vmstate_loading = false;
         vm_start();
         trace_colo_vm_state_change("stop", "run");
Signature · 1 line
2.17.GIT
author
#23 lore raw

PATCH v12 18/19 COLO: quick failover process by kick COLO thread

In reply to Zhang Chen

From: zhanghailiang <[email protected]>

COLO thread may sleep at qemu_sem_wait(&s->colo_checkpoint_sem), while failover works begin, It's better to wakeup it to quick the process.

---
 migration/colo.c | 8 ++++++++
 1 file changed, 8 insertions(+)
migration/colo.c +8 -0
index 365e913e51..3a3efb916b 100644
@@ -131,6 +131,11 @@ static void primary_vm_do_failover(void)
 
     migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
                       MIGRATION_STATUS_COMPLETED);
+    /*
+     * kick COLO thread which might wait at
+     * qemu_sem_wait(&s->colo_checkpoint_sem).
+     */
+    colo_checkpoint_notify(migrate_get_current());
 
     /*
      * Wake up COLO thread which may blocked in recv() or send(),
@@ -539,6 +544,9 @@ static void colo_process_checkpoint(MigrationState *s)
 
         qemu_sem_wait(&s->colo_checkpoint_sem);
 
+        if (s->state != MIGRATION_STATUS_COLO) {
+            goto out;
+        }
         ret = colo_do_checkpoint_transaction(s, bioc, fb);
         if (ret < 0) {
             goto out;
Signature · 1 line
2.17.GIT
author
#24 lore raw

PATCH v12 19/19 docs: Add COLO status diagram to COLO-FT.txt

In reply to Zhang Chen

From: Zhang Chen <[email protected]>

This diagram make user better understand COLO.
Suggested by Markus Armbruster.

---
 docs/COLO-FT.txt | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
docs/COLO-FT.txt +34 -0
index d7c7dcda8f..d5007895d1 100644
@@ -110,6 +110,40 @@ Note:
 HeartBeat has not been implemented yet, so you need to trigger failover process
 by using 'x-colo-lost-heartbeat' command.
 
+== COLO operation status ==
+
++-----------------+
+|                 |
+|    Start COLO   |
+|                 |
++--------+--------+
+         |
+         |  Main qmp command:
+         |  migrate-set-capabilities with x-colo
+         |  migrate
+         |
+         v
++--------+--------+
+|                 |
+|  COLO running   |
+|                 |
++--------+--------+
+         |
+         |  Main qmp command:
+         |  x-colo-lost-heartbeat
+         |  or
+         |  some error happened
+         v
++--------+--------+
+|                 |  send qmp event:
+|  COLO failover  |  COLO_EXIT
+|                 |
++-----------------+
+
+COLO use the qmp command switching and report operation status.
+The diagram just write the main qmp command, you can get the detail
+in test procedure.
+
 == Test procedure ==
 1. Startup qemu
 Primary:
Signature · 1 line
2.17.GIT
#25 lore raw

Re: [Qemu-devel] [PATCH V12 19/19] docs: Add COLO status diagram to COLO-FT.txt

Quoted 40 lines · Zhang Chen

From: Zhang Chen <[email protected]>

This diagram make user better understand COLO.
Suggested by Markus Armbruster.

---
  docs/COLO-FT.txt | 34 ++++++++++++++++++++++++++++++++++
  1 file changed, 34 insertions(+)
docs/COLO-FT.txt +21 -0
index d7c7dcda8f..d5007895d1 100644
@@ -110,6 +110,40 @@ Note:
  HeartBeat has not been implemented yet, so you need to trigger failover process
  by using 'x-colo-lost-heartbeat' command.
 
+== COLO operation status ==
+
++-----------------+
+|                 |
+|    Start COLO   |
+|                 |
++--------+--------+
+         |
+         |  Main qmp command:
+         |  migrate-set-capabilities with x-colo
+         |  migrate
+         |
+         v
++--------+--------+
+|                 |
+|  COLO running   |
+|                 |
++--------+--------+
+         |
+         |  Main qmp command:
+         |  x-colo-lost-heartbeat

docs/COLO-FT.txt Note: · line 143

Quoted 10 lines
docs/COLO-FT.txt +10 -0
+         |  or
+         |  some error happened
+         v
++--------+--------+
+|                 |  send qmp event:
+|  COLO failover  |  COLO_EXIT
+|                 |
++-----------------+
+
+COLO use the qmp command switching and report operation status.

It looks to me that "to switch" is better.

docs/COLO-FT.txt Note: · line 145

Quoted 2 lines
docs/COLO-FT.txt +2 -0
+The diagram just write the main qmp command, you can get the detail
+in test procedure.

s/write/shows/

I will fix this patch directly.

Thanks

Quoted 4 lines
docs/COLO-FT.txt +1 -0
+
  == Test procedure ==
  1. Startup qemu
  Primary:
author
#26 lore raw
In reply to Zhang Chen

Hi All.
Have any comments?
Ping...

Thanks
Zhang Chen

Quoted 135 lines · Zhang Chen

Hi~ All~

COLO Frame, block replication and COLO proxy(colo-compare,filter-mirror, filter-redirector,filter-rewriter) have been exist in qemu for long time, it's time to integrate these three parts to make COLO really works.

In this series, we have some optimizations for COLO frame, including separating the
process of saving ram and device state, using an COLO_EXIT event to notify users that
VM exits COLO, for these parts, most of them have been reviewed long time ago in old version,
but since this series have just rebased on upstream which had merged a new series of migration,
parts of pathes in this series deserve review again.

We use notifier/callback method for COLO compare to notify COLO frame about net packets inconsistent event, and add a handle_event method for NetFilterClass to
help COLO frame to notify filters and colo-compare about checkpoint/failover event,
it is flexible.

For the neweset version, please refer to:
https://github.com/zhangckid/qemu/tree/qemu-colo-18sep1

Please review, thanks.

V12:
- Rebased on upstream.
- Removed the patch 15/20 for feature work as Jason's comments.
- Fixed failover bugs in patch 16/19.
- Renamed the dummy_handle_event to default_handle_event in patch 15/19.
- Cleaned needless check job.

V11:
- Rebased on upstream.
- Used "RAMBLOCK_FOREACH_MIGRATABLE()" to replace "QLIST_FOREACH_RCU()"
in patch 08/20.
- Fixed COLO related qapi command's since version in patch 10/20.

V10:
- Rebased on upstream.
- Removed the "active" in COLOState.
- Fixed some comments.

V9:
- Rebased on upstream codes.
- Addressed Jason's comments add TCP state machine track in filter-rewriter.
- Fix some bug in colo-compare.
- Fix typo.
- Add filter-rewriter failover handle.
- Add net client type check in colo-compare.
- Add COLO state diagram.
- Addressed Markus and Daive's comments.

V8:
- Rebased on upstream codes.
- Addressed Markus's comments in patch 10/17.
- Addressed Markus's comments in patch 11/17.
- Removed some comments in patch 4/17.
- Moved the "migration_bitmap_clear_dirty()" to suitable position in patch 9/17.
- Rewrote the patch 07/17 to address Davie's comments.
- Moved the "qemu_savevm_live_state" out of the qemu_mutex_lock_iothread.
- Fixed the bug that in some status COLO vm crash with segmentation fault.

V7:
- Addressed Markus's comments in 11/17.
- Rebased on upstream.

V6:
- Addressed Eric Blake's comments, use the enum to feedback in patch
11/17.
- Fixed QAPI command separator problem in patch 11/17.

Zhang Chen (15):
filter-rewriter: Add TCP state machine and fix memory leak in
connection_track_table
colo-compare: implement the process of checkpoint
colo-compare: use notifier to notify packets comparing result
COLO: integrate colo compare with colo frame
COLO: Add block replication into colo process
COLO: Remove colo_state migration struct
COLO: Load dirty pages into SVM's RAM cache firstly ram/COLO: Record the dirty pages that SVM received
COLO: Flush memory data from ram cache qapi/migration.json: Rename COLO unknown mode to none mode.
qapi: Add new command to query colo status
savevm: split the process of different stages for loadvm/savevm
filter: Add handle_event method for NetFilterClass
filter-rewriter: handle checkpoint and failover event
docs: Add COLO status diagram to COLO-FT.txt

zhanghailiang (4):
qmp event: Add COLO_EXIT event to notify users while exited COLO
COLO: flush host dirty ram from cache
COLO: notify net filters about checkpoint/failover event
COLO: quick failover process by kick COLO thread

 docs/COLO-FT.txt          |  34 ++++++
 include/exec/ram_addr.h   |   1 +
 include/migration/colo.h  |  11 +-
 include/net/filter.h      |   5 +
 migration/Makefile.objs   |   2 +-
 migration/colo-comm.c     |  76 --------------
 migration/colo-failover.c |   2 +-
 migration/colo.c          | 212 ++++++++++++++++++++++++++++++++++++--
 migration/migration.c     |  46 ++++++++-
 migration/ram.c           | 166 ++++++++++++++++++++++++++++-
 migration/ram.h           |   4 +
 migration/savevm.c        |  53 ++++++++--
 migration/savevm.h        |   5 +
 migration/trace-events    |   3 +
 net/colo-compare.c        | 115 +++++++++++++++++++--
 net/colo-compare.h        |  24 +++++
 net/colo.c                |  10 +-
 net/colo.h                |  11 +-
 net/filter-rewriter.c     | 162 +++++++++++++++++++++++++++--
 net/filter.c              |  17 +++
 net/net.c                 |  19 ++++
 qapi/migration.json       |  80 +++++++++++++-
 vl.c                      |   2 -
 23 files changed, 921 insertions(+), 139 deletions(-)
 delete mode 100644 migration/colo-comm.c
 create mode 100644 net/colo-compare.h

--
2.17.GIT

#27 lore raw
Quoted 6 lines · Zhang Chen

Hi All.
Have any comments?
Ping...

Thanks
Zhang Chen

I've queued them with some tweaks on the commit log.

Please refer the comment of patch 1 and send me a patch on top for a better comment.

Thanks

author
#28 lore raw
Quoted 7 lines · Jason Wang
Quoted 4 lines · Zhang Chen

Hi All.
Have any comments?
Ping...

Quoted 9 lines
Quoted 2 lines

Thanks
Zhang Chen

I've queued them with some tweaks on the commit log.

Please refer the comment of patch 1 and send me a patch on top for a better comment.

OK. I got it.

Thanks
Zhang Chen

Quoted 3 lines

Thanks

author
#29 lore raw
Quoted 16 lines · Zhang Chen
Quoted 12 lines · Jason Wang
Quoted 6 lines · Zhang Chen

Hi All.
Have any comments?
Ping...

Thanks
Zhang Chen

I've queued them with some tweaks on the commit log.

Quoted 9 lines
Quoted 4 lines

Please refer the comment of patch 1 and send me a patch on top for a better comment.

OK. I got it.

Thanks
Zhang Chen

Hi Jason,

I have sent the new version patch 1.
What do you think about the new comments?

Thanks
Zhang Chen

Quoted 5 lines
Quoted 5 lines

Thanks