1 /*
2  * Copyright (C) 2016 FUJITSU LIMITED
3  * Author: Wen Congyang <wency@cn.fujitsu.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; version 2.1 only. with the special
8  * exception on linking described in file LICENSE.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  */
15 
16 #ifndef LIBXL_COLO_H
17 #define LIBXL_COLO_H
18 
19 #include "libxl_internal.h"
20 
21 /* Maximum time(5s) to wait for colo proxy checkpoit */
22 #define COLO_PROXY_CHECKPOINT_TIMEOUT 5000000
23 
24 #define ASYNC_CALL(egc, ao, child, param, func, callback) do {          \
25     int pid = -1;                                                       \
26     STATE_AO_GC(ao);                                                    \
27                                                                         \
28     pid = libxl__ev_child_fork(gc, child, callback);                    \
29     if (pid == -1) {                                                    \
30         LOGD(ERROR, ao->domid, "unable to fork");                       \
31         goto out;                                                       \
32     }                                                                   \
33                                                                         \
34     if (!pid) {                                                         \
35         /* child */                                                     \
36         func(param);                                                    \
37         /* notreached */                                                \
38         abort();                                                        \
39     }                                                                   \
40                                                                         \
41     return;                                                             \
42 out:                                                                    \
43     callback(egc, child, -1, 1);                                        \
44 } while (0)
45 
46 enum {
47     LIBXL_COLO_SETUPED,
48     LIBXL_COLO_SUSPENDED,
49     LIBXL_COLO_RESUMED,
50 };
51 
52 struct libxl__colo_device_nic {
53     int devid;
54     const char *vif;
55 };
56 
57 struct libxl__colo_qdisk {
58     bool setuped;
59 };
60 
61 struct libxl__colo_proxy_state {
62     /* set by caller of colo_proxy_setup */
63     struct libxl__ao *ao;
64 
65     int sock_fd;
66     int index;
67     /*
68      * Private, True means use userspace colo proxy
69      *          False means use kernel colo proxy.
70      */
71     bool is_userspace_proxy;
72     const char *checkpoint_host;
73     const char *checkpoint_port;
74 };
75 
76 struct libxl__colo_save_state {
77     int send_fd;
78     int recv_fd;
79     char *colo_proxy_script;
80 
81     /* private */
82     libxl__stream_read_state srs;
83     void (*callback)(libxl__egc *, libxl__colo_save_state *, int);
84     bool svm_running;
85     bool paused;
86 
87     /* private, used by qdisk block replication */
88     bool qdisk_used;
89     bool qdisk_setuped;
90 
91     /* private, used by colo-proxy */
92     libxl__colo_proxy_state cps;
93     libxl__ev_child child;
94 };
95 
96 
97 typedef void libxl__colo_callback(struct libxl__egc *egc,
98                                   libxl__colo_restore_state *crs, int rc);
99 
100 struct libxl__colo_restore_state {
101     /* must set by caller of libxl__colo_(setup|teardown) */
102     struct libxl__ao *ao;
103     uint32_t domid;
104     int send_back_fd;
105     int recv_fd;
106     int hvm;
107     libxl__colo_callback *callback;
108     char *colo_proxy_script;
109 
110     /* private, colo restore checkpoint state */
111     libxl__domain_create_cb *saved_cb;
112     void *crcs;
113 
114     /* private, used by qdisk block replication */
115     bool qdisk_used;
116     bool qdisk_setuped;
117     const char *host;
118     const char *port;
119 
120     /* private, used by colo-proxy */
121     libxl__colo_proxy_state cps;
122 };
123 
124 int init_subkind_qdisk(struct libxl__checkpoint_devices_state *cds);
125 
126 void cleanup_subkind_qdisk(struct libxl__checkpoint_devices_state *cds);
127 
128 int init_subkind_colo_nic(struct libxl__checkpoint_devices_state *cds);
129 
130 void cleanup_subkind_colo_nic(struct libxl__checkpoint_devices_state *cds);
131 
132 extern void libxl__colo_restore_setup(struct libxl__egc *egc,
133                                       libxl__colo_restore_state *crs);
134 extern void libxl__colo_restore_teardown(struct libxl__egc *egc, void *dcs_void,
135                                          int ret, int retval, int errnoval);
136 extern void libxl__colo_save_setup(struct libxl__egc *egc,
137                                    struct libxl__colo_save_state *css);
138 extern void libxl__colo_save_teardown(struct libxl__egc *egc,
139                                       struct libxl__colo_save_state *css,
140                                       int rc);
141 extern int colo_proxy_setup(libxl__colo_proxy_state *cps);
142 extern void colo_proxy_teardown(libxl__colo_proxy_state *cps);
143 extern void colo_proxy_preresume(libxl__colo_proxy_state *cps);
144 extern void colo_proxy_postresume(libxl__colo_proxy_state *cps);
145 extern int colo_proxy_checkpoint(libxl__colo_proxy_state *cps,
146                                  unsigned int timeout_us);
147 
148 #endif
149