Debian Codegraph @

docs/designs/non-cooperative-migration.md

raw
1 # Non-Cooperative Migration of Guests on Xen
2
3 ## Background
4
5 The normal model of migration in Xen is driven by the guest because it was
6 originally implemented for PV guests, where the guest must be aware it is
7 running under Xen and is hence expected to co-operate. This model dates from
8 an era when it was assumed that the host administrator had control of at
9 least the privileged software running in the guest (i.e. the guest kernel)
10 which may still be true in an enterprise deployment but is not generally
11 true in a cloud environment. The aim of this design is to provide a model
12 which is purely host driven, requiring no co-operation from the software
13 running in the guest, and is thus suitable for cloud scenarios.
14
15 PV guests are out of scope for this project because, as is outlined above,
16 they have a symbiotic relationship with the hypervisor and therefore a
17 certain level of co-operation is required.
18
19 x86 HVM guests can already be migrated on Xen without guest co-operation
20 but only if they don’t have PV drivers installed[1] or are not in ACPI
21 power state S0. The reason for not expecting co-operation if the guest is
22 any sort of suspended state is obvious, but the reason co-operation is
23 expected if PV drivers are installed is due to the nature of PV protocols.
24
25 ## Xenstore Nodes and Domain ID
26
27 The PV driver model consists of a *frontend* and a *backend*. The frontend
28 runs inside the guest domain and the backend runs inside a *service domain*
29 which may or may not be domain 0. The frontend and backend typically pass
30 data via memory pages which are shared between the two domains, but this
31 channel of communication is generally established using xenstore (the store
32 protocol itself being an exception to this for obvious chicken-and-egg
33 reasons).
34
35 Typical protocol establishment is based on use of two separate xenstore
36 *areas*. If we consider PV drivers for the *netif* protocol (i.e. class vif)
37 and assume the guest has domid X, the service domain has domid Y, and the
38 vif has index Z then the frontend area will reside under the parent node:
39
40 `/local/domain/Y/device/vif/Z`
41
42 All backends, by convention, typically reside under parent node:
43
44 `/local/domain/X/backend`
45
46 and the normal backend area for vif Z would be:
47
48 `/local/domain/X/backend/vif/Y/Z`
49
50 but this should not be assumed.
51
52 The toolstack will place two nodes in the frontend area to explicitly locate
53 the backend:
54
55 * `backend`: the fully qualified xenstore path of the backend area
56 * `backend-id`: the domid of the service domain
57
58 and similarly two nodes in the backend area to locate the frontend area:
59
60 * `frontend`: the fully qualified xenstore path of the frontend area
61 * `frontend-id`: the domid of the guest domain
62
63
64 The guest domain only has write permission to the frontend area and
65 similarly the service domain only has write permission to the backend area,
66 but both ends have read permission to both areas.
67
68 Under both frontend and backend areas is a node called *state*. This is key
69 to protocol establishment. Upon PV device creation the toolstack will set
70 the value of both state nodes to 1 (XenbusStateInitialising[2]). This
71 should cause enumeration of appropriate devices in both the guest and
72 service domains. The backend device, once it has written any necessary
73 protocol specific information into the xenstore backend area (to be read
74 by the frontend driver) will update the backend state node to 2
75 (XenbusStateInitWait). From this point on PV protocols differ slightly; the
76 following illustration is true of the netif protocol.
77
78 Upon seeing a backend state value of 2, the frontend driver will then read
79 the protocol specific information, write details of grant references (for
80 shared pages) and event channel ports (for signalling) that it has created,
81 and set the state node in the frontend area to 4 (XenbusStateConnected).
82 Upon see this frontend state, the backend driver will then read the grant
83 references (mapping the shared pages) and event channel ports (opening its
84 end of them) and set the state node in the backend area to 4. Protocol
85 establishment is now complete and the frontend and backend start to pass
86 data.
87
88 Because the domid of both ends of a PV protocol forms a key part of
89 negotiating the data plane for that protocol (because it is encoded into
90 both xenstore nodes and node paths), and because guest’s own domid and the
91 domid of the service domain are visible to the guest in xenstore (and hence
92 ay cached internally), and neither are necessarily preserved during
93 migration, it is hence necessary to have the co-operation of the frontend
94 in re-negotiating the protocol using the new domid after migration.
95
96 Moreover the backend-id value will be used by the frontend driver in
97 setting up grant table entries and event channels to communicate with the
98 service domain, so the co-operation of the guest is required to
99 re-establish these in the new host environment after migration.
100
101 Thus if we are to change the model and support migration of a guest with PV
102 drivers, without the co-operation of the frontend driver code, the paths and
103 values in both the frontend and backend xenstore areas must remain unchanged
104 and valid in the new host environment, and the grant table entries and event
105 channels must be preserved (and remain operational once guest execution is
106 resumed).
107
108 Because the service domain’s domid is used directly by the guest in setting
109 up grant entries and event channels, the backend drivers in the new host
110 environment must be provided by service domain with the same domid. Also,
111 because the guest can sample its own domid from the frontend area and use
112 it in hypercalls (e.g. HVMOP_set_param) rather than DOMID_SELF, the guest
113 domid must also be preserved to maintain the ABI.
114
115 Furthermore, it will be necessary to modify backend drivers to re-establish
116 communication with frontend drivers without perturbing the content of the
117 backend area or requiring any changes to the values of the xenstore state
118 nodes.
119
120 ## Other Para-Virtual State
121
122 ### Shared Rings
123
124 Because the console and store protocol shared pages are actually part of
125 the guest memory image (in an E820 reserved region just below 4G in x86
126 VMs) then the content will get migrated as part of the guest memory image.
127 Hence no additional code is require to prevent any guest visible change in
128 the content.
129
130 ### Shared Info
131
132 There is already a record defined in *libxenctrl Domain Image Format* [3]
133 called `SHARED_INFO` which simply contains a complete copy of the domain’s
134 shared info page. It is not currently incuded in an HVM (type `0x0002`)
135 migration stream. It may be feasible to include it as an optional record
136 but it is not clear that the content of the shared info page ever needs
137 to be preserved for an HVM guest.
138
139 For a PV guest the `arch_shared_info` sub-structure contains important
140 information about the guest’s P2M, but this information is not relevant for
141 an HVM guest where the P2M is not directly manipulated via the guest. The
142 other state contained in the `shared_info` structure relates the domain
143 wall-clock (the state of which should already be transferred by the `RTC`
144 HVM context information which contained in the `HVM_CONTEXT` save record)
145 and some event channel state (particularly if using the *2l* protocol).
146 Event channel state will need to be fully transferred if we are not going
147 to require the guest co-operation to re-open the channels and so it should
148 be possible to re-build a shared info page for an HVM guest from such other
149 state.
150
151 Note that the shared info page also contains an array of
152 `XEN_LEGACY_MAX_VCPUS` (32 for x86) `vcpu_info` structures. A domain may
153 nominate a different guest physical address to use for the vcpu info. This
154 is mandatory if a domain wants to use more than XEN_LEGACY_MAX_VCPUS vCPUs
155 and optional otherwise. This mapping is not currently transferred in the
156 migration state so this will either need to be added into an existing save
157 record, or an additional type of save record will be needed.
158
159 ### Xenstore Watches
160
161 As mentioned above, no domain Xenstore state is currently transferred in
162 the migration stream. There is a record defined in *libxenlight Domain
163 Image Format* [4] called `EMULATOR_XENSTORE_DATA` for transferring Xenstore
164 nodes relating to emulators but no record type is defined for nodes
165 relating to the domain itself, nor for registered *watches*. A XenStore
166 watch is a mechanism used by PV frontend and backend drivers to request a
167 notification if the value of a particular node (e.g. the other end’s state
168 node) changes, so it is important that watches continue to function after a
169 migration. One or more new save records will therefore be required to
170 transfer Xenstore state. It will also be necessary to extend the *store*
171 protocol[5] with mechanisms to allow the toolstack to acquire the list of
172 watches that the guest has registered and for the toolstack to register a
173 watch on behalf of a domain.
174
175 ### Event channels
176
177 Event channels are essentially the para-virtual equivalent of interrupts.
178 They are an important part of post PV protocols. Normally a frontend driver
179 creates an *inter-domain* event channel between its own domain and the
180 domain running the backend, which it discovers using the `backend-id` node
181 in Xenstore (see above), by making a `EVTCHNOP_alloc_unbound` hypercall.
182 This hypercall allocates an event channel object in the hypervisor and
183 assigns a *local port* number which is then written into the frontend area
184 in Xenstore. The backend driver then reads this port number and *binds* to
185 the event channel by specifying it, and the value of `frontend-id`, as
186 *remote domain* and *remote port* (respectively) to a
187 `EVTCHNOP_bind_interdomain` hypercall. Once connection is established in
188 this fashion frontend and backend drivers can use the event channel as a
189 *mailbox* to notify each other when a shared ring has been updated with new
190 requests or response structures.
191
192 Currently no event channel state is preserved on migration, requiring
193 frontend and backend drivers to create and bind a complete new set of event
194 channels in order to re-establish a protocol connection. Hence, one or more
195 new save records will be required to transfer event channel state in order
196 to avoid the need for explicit action by frontend drivers running in the
197 guest. Note that the local port numbers need to preserved in this state as
198 they are the only context the guest has to refer to the hypervisor event
199 channel objects.
200
201 Note also that the PV *store* (Xenstore access) and *console* protocols
202 also rely on event channels which are set up by the toolstack. Normally,
203 early in migration, the toolstack running on the remote host would set up a
204 new pair of event channels for these protocols in the destination domain.
205 These may not be assigned the same local port numbers as the protocols
206 running in the source domain. For non-cooperative migration these channels
207 must either be created with fixed port numbers, or their creation must be
208 avoided and instead be included in the general event channel state
209 record(s).
210
211 ### Grant table
212
213 The grant table is essentially the para-virtual equivalent of an IOMMU. For
214 example, the shared rings of a PV protocol are *granted* by a frontend
215 driver to the backend driver by allocating *grant entries* in the guest’s
216 table, filling in details of the memory pages and then writing the *grant
217 references* (the index values of the grant entries) into Xenstore. The
218 grant references of the protocol buffers themselves are typically written
219 directly into the request structures passed via a shared ring.
220
221 The guest is responsible for managing its own grant table. No hypercall is
222 required to grant a memory page to another domain. It is sufficient to find
223 an unused grant entry and set bits in the entry to give read and/or write
224 access to a remote domain also specified in the entry along with the page
225 frame number. Thus the layout and content of the grant table logically
226 forms part of the guest state.
227
228 Currently no grant table state is migrated, requiring a guest to separately
229 maintain any state that it wishes to persist elsewhere in its memory image
230 and then restore it after migration. Thus to avoid the need for such
231 explicit action by the guest, one or more new save records will be required
232 to migrate the contents of the grant table.
233
234 # Outline Proposal
235
236 * PV backend drivers will be modified to unilaterally re-establish
237 connection to a frontend if the backend state node is restored with value 4
238 (XenbusStateConnected)[6].
239
240 * The toolstack choose a randomized domid for initial creation or default
241 migration, but preserve the source domid non-cooperative migration.
242 Non-Cooperative migration will have to be denied if the domid is
243 unavailable on the target host, but randomization of domid on creation
244 should hopefully minimize the likelihood of this. Non-Cooperative migration
245 to localhost will clearly not be possible.
246
247 * `xenstored` should be modified to implement the new mechanisms needed.
248 See *Other Para-Virtual State* above. A further design document will
249 propose additional protocol messages.
250
251 * Within the migration stream extra save records will be defined as
252 required. See *Other Para-Virtual State* above. A further design document
253 will propose modifications to the libxenlight and libxenctrl Domain Image
254 Formats.
255
256 * An option should be added to the toolstack to initiate a non-cooperative
257 migration, instead of the (default) potentially co-operative migration.
258 Essentially this should skip the check to see if PV drivers and migrate as
259 if there are none present, but also enabling the extra save records. Note
260 that at least some of the extra records should only form part of a
261 non-cooperative migration stream. For example, migrating event channel
262 state would be counter-productive in a normal migration as this will
263 essentially leak event channel objects at the receiving end. Others, such
264 as grant table state, could potentially harmlessly form part of a normal
265 migration stream.
266
267 * * *
268 [1] PV drivers are deemed to be installed if the HVM parameter
269 *HVM_PARAM_CALLBACK_IRQ* has been set to a non-zero value.
270
271 [2] See https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/include/public/io/xenbus.h
272
273 [3] See https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=docs/specs/libxc-migration-stream.pandoc
274
275 [4] See https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=docs/specs/libxl-migration-stream.pandoc
276
277 [5] See https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=docs/misc/xenstore.txt
278
279 [6] `xen-blkback` and `xen-netback` have already been modified in Linux to do
280 this.