Commit | Line | Data |
---|---|---|
96dd86fa S |
1 | /* |
2 | * An implementation of host initiated guest snapshot. | |
3 | * | |
4 | * | |
5 | * Copyright (C) 2013, Microsoft, Inc. | |
6 | * Author : K. Y. Srinivasan <kys@microsoft.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License version 2 as published | |
10 | * by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
15 | * NON INFRINGEMENT. See the GNU General Public License for more | |
16 | * details. | |
17 | * | |
18 | */ | |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
20 | ||
21 | #include <linux/net.h> | |
22 | #include <linux/nls.h> | |
23 | #include <linux/connector.h> | |
24 | #include <linux/workqueue.h> | |
25 | #include <linux/hyperv.h> | |
26 | ||
3647a83d VK |
27 | #include "hyperv_vmbus.h" |
28 | ||
6741335b S |
29 | #define VSS_MAJOR 5 |
30 | #define VSS_MINOR 0 | |
3a491605 | 31 | #define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR) |
6741335b | 32 | |
64914207 | 33 | #define VSS_USERSPACE_TIMEOUT (msecs_to_jiffies(10 * 1000)) |
96dd86fa S |
34 | |
35 | /* | |
36 | * Global state maintained for transaction that is being processed. | |
37 | * Note that only one transaction can be active at any point in time. | |
38 | * | |
39 | * This state is set when we receive a request from the host; we | |
40 | * cleanup this state when the transaction is completed - when we respond | |
41 | * to the host with the key value. | |
42 | */ | |
43 | ||
44 | static struct { | |
45 | bool active; /* transaction status - active or not */ | |
46 | int recv_len; /* number of bytes received. */ | |
47 | struct vmbus_channel *recv_channel; /* chn we got the request */ | |
48 | u64 recv_req_id; /* request ID. */ | |
49 | struct hv_vss_msg *msg; /* current message */ | |
50 | } vss_transaction; | |
51 | ||
52 | ||
53 | static void vss_respond_to_host(int error); | |
54 | ||
55 | static struct cb_id vss_id = { CN_VSS_IDX, CN_VSS_VAL }; | |
56 | static const char vss_name[] = "vss_kernel_module"; | |
57 | static __u8 *recv_buffer; | |
58 | ||
59 | static void vss_send_op(struct work_struct *dummy); | |
64914207 VK |
60 | static void vss_timeout_func(struct work_struct *dummy); |
61 | ||
62 | static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func); | |
96dd86fa S |
63 | static DECLARE_WORK(vss_send_op_work, vss_send_op); |
64 | ||
65 | /* | |
66 | * Callback when data is received from user mode. | |
67 | */ | |
68 | ||
64914207 VK |
69 | static void vss_timeout_func(struct work_struct *dummy) |
70 | { | |
71 | /* | |
72 | * Timeout waiting for userspace component to reply happened. | |
73 | */ | |
74 | pr_warn("VSS: timeout waiting for daemon to reply\n"); | |
75 | vss_respond_to_host(HV_E_FAIL); | |
76 | } | |
77 | ||
96dd86fa S |
78 | static void |
79 | vss_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) | |
80 | { | |
81 | struct hv_vss_msg *vss_msg; | |
82 | ||
83 | vss_msg = (struct hv_vss_msg *)msg->data; | |
84 | ||
85 | if (vss_msg->vss_hdr.operation == VSS_OP_REGISTER) { | |
86 | pr_info("VSS daemon registered\n"); | |
87 | vss_transaction.active = false; | |
88 | if (vss_transaction.recv_channel != NULL) | |
89 | hv_vss_onchannelcallback(vss_transaction.recv_channel); | |
90 | return; | |
91 | ||
92 | } | |
64914207 VK |
93 | if (cancel_delayed_work_sync(&vss_timeout_work)) |
94 | vss_respond_to_host(vss_msg->error); | |
96dd86fa S |
95 | } |
96 | ||
97 | ||
98 | static void vss_send_op(struct work_struct *dummy) | |
99 | { | |
100 | int op = vss_transaction.msg->vss_hdr.operation; | |
8d9560eb | 101 | int rc; |
96dd86fa S |
102 | struct cn_msg *msg; |
103 | struct hv_vss_msg *vss_msg; | |
104 | ||
105 | msg = kzalloc(sizeof(*msg) + sizeof(*vss_msg), GFP_ATOMIC); | |
106 | if (!msg) | |
107 | return; | |
108 | ||
109 | vss_msg = (struct hv_vss_msg *)msg->data; | |
110 | ||
111 | msg->id.idx = CN_VSS_IDX; | |
112 | msg->id.val = CN_VSS_VAL; | |
113 | ||
114 | vss_msg->vss_hdr.operation = op; | |
115 | msg->len = sizeof(struct hv_vss_msg); | |
116 | ||
8d9560eb VK |
117 | rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC); |
118 | if (rc) { | |
119 | pr_warn("VSS: failed to communicate to the daemon: %d\n", rc); | |
120 | if (cancel_delayed_work_sync(&vss_timeout_work)) | |
121 | vss_respond_to_host(HV_E_FAIL); | |
122 | } | |
96dd86fa S |
123 | kfree(msg); |
124 | ||
125 | return; | |
126 | } | |
127 | ||
128 | /* | |
129 | * Send a response back to the host. | |
130 | */ | |
131 | ||
132 | static void | |
133 | vss_respond_to_host(int error) | |
134 | { | |
135 | struct icmsg_hdr *icmsghdrp; | |
136 | u32 buf_len; | |
137 | struct vmbus_channel *channel; | |
138 | u64 req_id; | |
139 | ||
140 | /* | |
141 | * If a transaction is not active; log and return. | |
142 | */ | |
143 | ||
144 | if (!vss_transaction.active) { | |
145 | /* | |
146 | * This is a spurious call! | |
147 | */ | |
148 | pr_warn("VSS: Transaction not active\n"); | |
149 | return; | |
150 | } | |
151 | /* | |
152 | * Copy the global state for completing the transaction. Note that | |
153 | * only one transaction can be active at a time. | |
154 | */ | |
155 | ||
156 | buf_len = vss_transaction.recv_len; | |
157 | channel = vss_transaction.recv_channel; | |
158 | req_id = vss_transaction.recv_req_id; | |
159 | vss_transaction.active = false; | |
160 | ||
161 | icmsghdrp = (struct icmsg_hdr *) | |
162 | &recv_buffer[sizeof(struct vmbuspipe_hdr)]; | |
163 | ||
164 | if (channel->onchannel_callback == NULL) | |
165 | /* | |
166 | * We have raced with util driver being unloaded; | |
167 | * silently return. | |
168 | */ | |
169 | return; | |
170 | ||
171 | icmsghdrp->status = error; | |
172 | ||
173 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; | |
174 | ||
175 | vmbus_sendpacket(channel, recv_buffer, buf_len, req_id, | |
176 | VM_PKT_DATA_INBAND, 0); | |
177 | ||
178 | } | |
179 | ||
180 | /* | |
181 | * This callback is invoked when we get a VSS message from the host. | |
182 | * The host ensures that only one VSS transaction can be active at a time. | |
183 | */ | |
184 | ||
185 | void hv_vss_onchannelcallback(void *context) | |
186 | { | |
187 | struct vmbus_channel *channel = context; | |
188 | u32 recvlen; | |
189 | u64 requestid; | |
190 | struct hv_vss_msg *vss_msg; | |
191 | ||
192 | ||
193 | struct icmsg_hdr *icmsghdrp; | |
194 | struct icmsg_negotiate *negop = NULL; | |
195 | ||
196 | if (vss_transaction.active) { | |
197 | /* | |
198 | * We will defer processing this callback once | |
199 | * the current transaction is complete. | |
200 | */ | |
201 | vss_transaction.recv_channel = channel; | |
202 | return; | |
203 | } | |
204 | ||
205 | vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen, | |
206 | &requestid); | |
207 | ||
208 | if (recvlen > 0) { | |
209 | icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ | |
210 | sizeof(struct vmbuspipe_hdr)]; | |
211 | ||
212 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
213 | vmbus_prep_negotiate_resp(icmsghdrp, negop, | |
3a491605 S |
214 | recv_buffer, UTIL_FW_VERSION, |
215 | VSS_VERSION); | |
96dd86fa S |
216 | } else { |
217 | vss_msg = (struct hv_vss_msg *)&recv_buffer[ | |
218 | sizeof(struct vmbuspipe_hdr) + | |
219 | sizeof(struct icmsg_hdr)]; | |
220 | ||
221 | /* | |
222 | * Stash away this global state for completing the | |
223 | * transaction; note transactions are serialized. | |
224 | */ | |
225 | ||
226 | vss_transaction.recv_len = recvlen; | |
227 | vss_transaction.recv_channel = channel; | |
228 | vss_transaction.recv_req_id = requestid; | |
229 | vss_transaction.active = true; | |
230 | vss_transaction.msg = (struct hv_vss_msg *)vss_msg; | |
231 | ||
232 | switch (vss_msg->vss_hdr.operation) { | |
233 | /* | |
234 | * Initiate a "freeze/thaw" | |
235 | * operation in the guest. | |
236 | * We respond to the host once | |
237 | * the operation is complete. | |
238 | * | |
239 | * We send the message to the | |
240 | * user space daemon and the | |
241 | * operation is performed in | |
242 | * the daemon. | |
243 | */ | |
244 | case VSS_OP_FREEZE: | |
245 | case VSS_OP_THAW: | |
246 | schedule_work(&vss_send_op_work); | |
64914207 VK |
247 | schedule_delayed_work(&vss_timeout_work, |
248 | VSS_USERSPACE_TIMEOUT); | |
96dd86fa S |
249 | return; |
250 | ||
251 | case VSS_OP_HOT_BACKUP: | |
252 | vss_msg->vss_cf.flags = | |
253 | VSS_HBU_NO_AUTO_RECOVERY; | |
254 | vss_respond_to_host(0); | |
255 | return; | |
256 | ||
257 | case VSS_OP_GET_DM_INFO: | |
258 | vss_msg->dm_info.flags = 0; | |
259 | vss_respond_to_host(0); | |
260 | return; | |
261 | ||
262 | default: | |
263 | vss_respond_to_host(0); | |
264 | return; | |
265 | ||
266 | } | |
267 | ||
268 | } | |
269 | ||
270 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
271 | | ICMSGHDRFLAG_RESPONSE; | |
272 | ||
273 | vmbus_sendpacket(channel, recv_buffer, | |
274 | recvlen, requestid, | |
275 | VM_PKT_DATA_INBAND, 0); | |
276 | } | |
277 | ||
278 | } | |
279 | ||
280 | int | |
281 | hv_vss_init(struct hv_util_service *srv) | |
282 | { | |
283 | int err; | |
284 | ||
285 | err = cn_add_callback(&vss_id, vss_name, vss_cn_callback); | |
286 | if (err) | |
287 | return err; | |
288 | recv_buffer = srv->recv_buffer; | |
289 | ||
290 | /* | |
291 | * When this driver loads, the user level daemon that | |
292 | * processes the host requests may not yet be running. | |
293 | * Defer processing channel callbacks until the daemon | |
294 | * has registered. | |
295 | */ | |
296 | vss_transaction.active = true; | |
297 | return 0; | |
298 | } | |
299 | ||
300 | void hv_vss_deinit(void) | |
301 | { | |
302 | cn_del_callback(&vss_id); | |
64914207 | 303 | cancel_delayed_work_sync(&vss_timeout_work); |
96dd86fa S |
304 | cancel_work_sync(&vss_send_op_work); |
305 | } |