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 #include "libxl_osdeps.h" /* must come before any other headers */
17 
18 #include "libxl_internal.h"
19 
20 enum {
21     primary,
22     secondary,
23 };
24 
25 /* ========== init() and cleanup() ========== */
26 
init_subkind_colo_nic(libxl__checkpoint_devices_state * cds)27 int init_subkind_colo_nic(libxl__checkpoint_devices_state *cds)
28 {
29     return 0;
30 }
31 
cleanup_subkind_colo_nic(libxl__checkpoint_devices_state * cds)32 void cleanup_subkind_colo_nic(libxl__checkpoint_devices_state *cds)
33 {
34 }
35 
36 /* ========== helper functions ========== */
37 
38 static void colo_save_setup_script_cb(libxl__egc *egc,
39                                      libxl__async_exec_state *aes,
40                                      int rc, int status);
41 static void colo_save_teardown_script_cb(libxl__egc *egc,
42                                          libxl__async_exec_state *aes,
43                                          int rc, int status);
44 
45 /*
46  * If the device has a vifname, then use that instead of
47  * the vifX.Y format.
48  * it must ONLY be used for remus because if driver domains
49  * were in use it would constitute a security vulnerability.
50  */
get_vifname(libxl__checkpoint_device * dev,const libxl_device_nic * nic)51 static const char *get_vifname(libxl__checkpoint_device *dev,
52                                const libxl_device_nic *nic)
53 {
54     const char *vifname = NULL;
55     const char *path;
56     int rc;
57 
58     STATE_AO_GC(dev->cds->ao);
59 
60     /* Convenience aliases */
61     const uint32_t domid = dev->cds->domid;
62 
63     path = GCSPRINTF("%s/backend/vif/%d/%d/vifname",
64                      libxl__xs_get_dompath(gc, 0), domid, nic->devid);
65     rc = libxl__xs_read_checked(gc, XBT_NULL, path, &vifname);
66     if (!rc && !vifname) {
67         vifname = libxl__device_nic_devname(gc, domid,
68                                             nic->devid,
69                                             nic->nictype);
70     }
71 
72     return vifname;
73 }
74 
75 /*
76  * the script needs the following env & args
77  * $vifname
78  * $forwarddev
79  * $mode(primary/secondary)
80  * $index
81  * $bridge
82  * setup/teardown as command line arg.
83  */
setup_async_exec(libxl__checkpoint_device * dev,char * op,libxl__colo_proxy_state * cps,int side,char * colo_proxy_script)84 static void setup_async_exec(libxl__checkpoint_device *dev, char *op,
85                              libxl__colo_proxy_state *cps, int side,
86                              char *colo_proxy_script)
87 {
88     int arraysize, nr = 0;
89     char **env = NULL, **args = NULL;
90     libxl__colo_device_nic *colo_nic = dev->concrete_data;
91     libxl__checkpoint_devices_state *cds = dev->cds;
92     libxl__async_exec_state *aes = &dev->aodev.aes;
93     const libxl_device_nic *nic = dev->backend_dev;
94 
95     STATE_AO_GC(cds->ao);
96 
97     /* Convenience aliases */
98     const char *const vif = colo_nic->vif;
99 
100     arraysize = 11;
101     GCNEW_ARRAY(env, arraysize);
102     env[nr++] = "vifname";
103     env[nr++] = libxl__strdup(gc, vif);
104     env[nr++] = "forwarddev";
105     env[nr++] = libxl__strdup(gc, nic->coloft_forwarddev);
106     env[nr++] = "mode";
107     if (side == primary)
108         env[nr++] = "primary";
109     else
110         env[nr++] = "secondary";
111     env[nr++] = "index";
112     env[nr++] = GCSPRINTF("%d", cps->index);
113     env[nr++] = "bridge";
114     env[nr++] = libxl__strdup(gc, nic->bridge);
115     env[nr++] = NULL;
116     assert(nr == arraysize);
117 
118     arraysize = 3; nr = 0;
119     GCNEW_ARRAY(args, arraysize);
120     args[nr++] = colo_proxy_script;
121     args[nr++] = op;
122     args[nr++] = NULL;
123     assert(nr == arraysize);
124 
125     aes->ao = dev->cds->ao;
126     aes->what = GCSPRINTF("%s %s", args[0], args[1]);
127     aes->env = env;
128     aes->args = args;
129     aes->timeout_ms = LIBXL_HOTPLUG_TIMEOUT * 1000;
130     aes->stdfds[0] = -1;
131     aes->stdfds[1] = -1;
132     aes->stdfds[2] = -1;
133 
134     if (!strcmp(op, "teardown"))
135         aes->callback = colo_save_teardown_script_cb;
136     else
137         aes->callback = colo_save_setup_script_cb;
138 }
139 
140 /* ========== setup() and teardown() ========== */
141 
colo_nic_setup(libxl__egc * egc,libxl__checkpoint_device * dev,libxl__colo_proxy_state * cps,int side,char * colo_proxy_script)142 static void colo_nic_setup(libxl__egc *egc, libxl__checkpoint_device *dev,
143                            libxl__colo_proxy_state *cps, int side,
144                            char *colo_proxy_script)
145 {
146     int rc;
147     libxl__colo_device_nic *colo_nic;
148     const libxl_device_nic *nic = dev->backend_dev;
149 
150     STATE_AO_GC(dev->cds->ao);
151 
152     /*
153      * thers's no subkind of nic devices, so nic ops is always matched
154      * with nic devices, we begin to setup the nic device
155      */
156     dev->matched = 1;
157 
158     if (!nic->coloft_forwarddev) {
159         rc = ERROR_FAIL;
160         goto out;
161     }
162 
163     GCNEW(colo_nic);
164     dev->concrete_data = colo_nic;
165     colo_nic->devid = nic->devid;
166     colo_nic->vif = get_vifname(dev, nic);
167     if (!colo_nic->vif) {
168         rc = ERROR_FAIL;
169         goto out;
170     }
171 
172     setup_async_exec(dev, "setup", cps, side, colo_proxy_script);
173     rc = libxl__async_exec_start(&dev->aodev.aes);
174     if (rc)
175         goto out;
176 
177     return;
178 
179 out:
180     dev->aodev.rc = rc;
181     dev->aodev.callback(egc, &dev->aodev);
182 }
183 
colo_save_setup_script_cb(libxl__egc * egc,libxl__async_exec_state * aes,int rc,int status)184 static void colo_save_setup_script_cb(libxl__egc *egc,
185                                       libxl__async_exec_state *aes,
186                                       int rc, int status)
187 {
188     libxl__ao_device *aodev = CONTAINER_OF(aes, *aodev, aes);
189     libxl__checkpoint_device *dev = CONTAINER_OF(aodev, *dev, aodev);
190     libxl__colo_device_nic *colo_nic = dev->concrete_data;
191     libxl__checkpoint_devices_state *cds = dev->cds;
192     const char *out_path_base, *hotplug_error = NULL;
193 
194     EGC_GC;
195 
196     /* Convenience aliases */
197     const uint32_t domid = cds->domid;
198     const int devid = colo_nic->devid;
199     const char *const vif = colo_nic->vif;
200 
201     if (status && !rc)
202         rc = ERROR_FAIL;
203     if (rc)
204         goto out;
205 
206     out_path_base = GCSPRINTF("%s/colo_proxy/%d",
207                               libxl__xs_libxl_path(gc, domid), devid);
208 
209     rc = libxl__xs_read_checked(gc, XBT_NULL,
210                                 GCSPRINTF("%s/hotplug-error", out_path_base),
211                                 &hotplug_error);
212     if (rc)
213         goto out;
214 
215     if (hotplug_error) {
216         LOGD(ERROR, domid, "colo_proxy script %s setup failed for vif %s: %s",
217             aes->args[0], vif, hotplug_error);
218         rc = ERROR_FAIL;
219         goto out;
220     }
221 
222     rc = 0;
223 
224 out:
225     aodev->rc = rc;
226     aodev->callback(egc, aodev);
227 }
228 
colo_nic_teardown(libxl__egc * egc,libxl__checkpoint_device * dev,libxl__colo_proxy_state * cps,int side,char * colo_proxy_script)229 static void colo_nic_teardown(libxl__egc *egc, libxl__checkpoint_device *dev,
230                               libxl__colo_proxy_state *cps, int side,
231                               char *colo_proxy_script)
232 {
233     int rc;
234     libxl__colo_device_nic *colo_nic = dev->concrete_data;
235 
236     if (!colo_nic || !colo_nic->vif) {
237         /* colo nic has not yet been set up, just return */
238         rc = 0;
239         goto out;
240     }
241 
242     setup_async_exec(dev, "teardown", cps, side, colo_proxy_script);
243 
244     rc = libxl__async_exec_start(&dev->aodev.aes);
245     if (rc)
246         goto out;
247 
248     return;
249 
250 out:
251     dev->aodev.rc = rc;
252     dev->aodev.callback(egc, &dev->aodev);
253 }
254 
colo_save_teardown_script_cb(libxl__egc * egc,libxl__async_exec_state * aes,int rc,int status)255 static void colo_save_teardown_script_cb(libxl__egc *egc,
256                                          libxl__async_exec_state *aes,
257                                          int rc, int status)
258 {
259     libxl__ao_device *aodev = CONTAINER_OF(aes, *aodev, aes);
260 
261     if (status && !rc)
262         rc = ERROR_FAIL;
263     else
264         rc = 0;
265 
266     aodev->rc = rc;
267     aodev->callback(egc, aodev);
268 }
269 
270 /* ======== primary ======== */
271 
colo_nic_save_setup(libxl__egc * egc,libxl__checkpoint_device * dev)272 static void colo_nic_save_setup(libxl__egc *egc, libxl__checkpoint_device *dev)
273 {
274     libxl__colo_save_state *css = dev->cds->concrete_data;
275 
276     colo_nic_setup(egc, dev, &css->cps, primary, css->colo_proxy_script);
277 }
278 
colo_nic_save_teardown(libxl__egc * egc,libxl__checkpoint_device * dev)279 static void colo_nic_save_teardown(libxl__egc *egc,
280                                    libxl__checkpoint_device *dev)
281 {
282     libxl__colo_save_state *css = dev->cds->concrete_data;
283 
284     colo_nic_teardown(egc, dev, &css->cps, primary, css->colo_proxy_script);
285 }
286 
287 const libxl__checkpoint_device_instance_ops colo_save_device_nic = {
288     .kind = LIBXL__DEVICE_KIND_VIF,
289     .setup = colo_nic_save_setup,
290     .teardown = colo_nic_save_teardown,
291 };
292 
293 /* ======== secondary ======== */
294 
colo_nic_restore_setup(libxl__egc * egc,libxl__checkpoint_device * dev)295 static void colo_nic_restore_setup(libxl__egc *egc,
296                                    libxl__checkpoint_device *dev)
297 {
298     libxl__colo_restore_state *crs = dev->cds->concrete_data;
299 
300     colo_nic_setup(egc, dev, &crs->cps, secondary, crs->colo_proxy_script);
301 }
302 
colo_nic_restore_teardown(libxl__egc * egc,libxl__checkpoint_device * dev)303 static void colo_nic_restore_teardown(libxl__egc *egc,
304                                       libxl__checkpoint_device *dev)
305 {
306     libxl__colo_restore_state *crs = dev->cds->concrete_data;
307 
308     colo_nic_teardown(egc, dev, &crs->cps, secondary, crs->colo_proxy_script);
309 }
310 
311 const libxl__checkpoint_device_instance_ops colo_restore_device_nic = {
312     .kind = LIBXL__DEVICE_KIND_VIF,
313     .setup = colo_nic_restore_setup,
314     .teardown = colo_nic_restore_teardown,
315 };
316