blob: 2c8c246d09eb7371d0c8711d6f4cf0b033dc00ef [file] [log] [blame]
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -07001/*
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
Vitaly Kuznetsov3647a832015-04-11 18:07:39 -070027#include "hyperv_vmbus.h"
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -070028#include "hv_utils_transport.h"
Vitaly Kuznetsov3647a832015-04-11 18:07:39 -070029
K. Y. Srinivasan67413352013-07-02 10:31:30 -070030#define VSS_MAJOR 5
31#define VSS_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070032#define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
K. Y. Srinivasan67413352013-07-02 10:31:30 -070033
Vitaly Kuznetsov64914202014-11-06 18:21:24 +010034#define VSS_USERSPACE_TIMEOUT (msecs_to_jiffies(10 * 1000))
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070035
36/*
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -070037 * Global state maintained for transaction that is being processed. For a class
38 * of integration services, including the "VSS service", the specified protocol
39 * is a "request/response" protocol which means that there can only be single
40 * outstanding transaction from the host at any given point in time. We use
41 * this to simplify memory management in this driver - we cache and process
42 * only one message at a time.
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070043 *
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -070044 * While the request/response protocol is guaranteed by the host, we further
45 * ensure this by serializing packet processing in this driver - we do not
46 * read additional packets from the VMBUs until the current packet is fully
47 * handled.
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070048 */
49
50static struct {
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -070051 int state; /* hvutil_device_state */
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070052 int recv_len; /* number of bytes received. */
53 struct vmbus_channel *recv_channel; /* chn we got the request */
54 u64 recv_req_id; /* request ID. */
55 struct hv_vss_msg *msg; /* current message */
Vitaly Kuznetsov38c06c22015-04-11 18:07:43 -070056 void *vss_context; /* for the channel callback */
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070057} vss_transaction;
58
59
60static void vss_respond_to_host(int error);
61
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -070062static const char vss_devname[] = "vmbus/hv_vss";
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070063static __u8 *recv_buffer;
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -070064static struct hvutil_transport *hvt;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070065
66static void vss_send_op(struct work_struct *dummy);
Vitaly Kuznetsov64914202014-11-06 18:21:24 +010067static void vss_timeout_func(struct work_struct *dummy);
68
69static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070070static DECLARE_WORK(vss_send_op_work, vss_send_op);
71
72/*
73 * Callback when data is received from user mode.
74 */
75
Vitaly Kuznetsov64914202014-11-06 18:21:24 +010076static void vss_timeout_func(struct work_struct *dummy)
77{
78 /*
79 * Timeout waiting for userspace component to reply happened.
80 */
81 pr_warn("VSS: timeout waiting for daemon to reply\n");
82 vss_respond_to_host(HV_E_FAIL);
Vitaly Kuznetsov38c06c22015-04-11 18:07:43 -070083
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -070084 /* Transaction is finished, reset the state. */
85 if (vss_transaction.state > HVUTIL_READY)
86 vss_transaction.state = HVUTIL_READY;
87
Vitaly Kuznetsov38c06c22015-04-11 18:07:43 -070088 hv_poll_channel(vss_transaction.vss_context,
89 hv_vss_onchannelcallback);
Vitaly Kuznetsov64914202014-11-06 18:21:24 +010090}
91
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -070092static int vss_on_msg(void *msg, int len)
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070093{
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -070094 struct hv_vss_msg *vss_msg = (struct hv_vss_msg *)msg;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070095
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -070096 if (len != sizeof(*vss_msg))
97 return -EINVAL;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -070098
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -070099 /*
100 * Don't process registration messages if we're in the middle of
101 * a transaction processing.
102 */
103 if (vss_transaction.state > HVUTIL_READY &&
104 vss_msg->vss_hdr.operation == VSS_OP_REGISTER)
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700105 return -EINVAL;
Vitaly Kuznetsov38c06c22015-04-11 18:07:43 -0700106
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700107 if (vss_transaction.state == HVUTIL_DEVICE_INIT &&
108 vss_msg->vss_hdr.operation == VSS_OP_REGISTER) {
109 pr_info("VSS daemon registered\n");
110 vss_transaction.state = HVUTIL_READY;
111 } else if (vss_transaction.state == HVUTIL_USERSPACE_REQ) {
112 vss_transaction.state = HVUTIL_USERSPACE_RECV;
113 if (cancel_delayed_work_sync(&vss_timeout_work)) {
114 vss_respond_to_host(vss_msg->error);
115 /* Transaction is finished, reset the state. */
116 vss_transaction.state = HVUTIL_READY;
117 hv_poll_channel(vss_transaction.vss_context,
118 hv_vss_onchannelcallback);
119 }
120 } else {
121 /* This is a spurious call! */
122 pr_warn("VSS: Transaction not active\n");
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700123 return -EINVAL;
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700124 }
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700125 return 0;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700126}
127
128
129static void vss_send_op(struct work_struct *dummy)
130{
131 int op = vss_transaction.msg->vss_hdr.operation;
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100132 int rc;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700133 struct hv_vss_msg *vss_msg;
134
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700135 /* The transaction state is wrong. */
136 if (vss_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
137 return;
138
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700139 vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL);
140 if (!vss_msg)
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700141 return;
142
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700143 vss_msg->vss_hdr.operation = op;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700144
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700145 vss_transaction.state = HVUTIL_USERSPACE_REQ;
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700146 rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg));
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100147 if (rc) {
148 pr_warn("VSS: failed to communicate to the daemon: %d\n", rc);
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700149 if (cancel_delayed_work_sync(&vss_timeout_work)) {
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100150 vss_respond_to_host(HV_E_FAIL);
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700151 vss_transaction.state = HVUTIL_READY;
152 }
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100153 }
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700154
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700155 kfree(vss_msg);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700156
157 return;
158}
159
160/*
161 * Send a response back to the host.
162 */
163
164static void
165vss_respond_to_host(int error)
166{
167 struct icmsg_hdr *icmsghdrp;
168 u32 buf_len;
169 struct vmbus_channel *channel;
170 u64 req_id;
171
172 /*
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700173 * Copy the global state for completing the transaction. Note that
174 * only one transaction can be active at a time.
175 */
176
177 buf_len = vss_transaction.recv_len;
178 channel = vss_transaction.recv_channel;
179 req_id = vss_transaction.recv_req_id;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700180
181 icmsghdrp = (struct icmsg_hdr *)
182 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
183
184 if (channel->onchannel_callback == NULL)
185 /*
186 * We have raced with util driver being unloaded;
187 * silently return.
188 */
189 return;
190
191 icmsghdrp->status = error;
192
193 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
194
195 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
196 VM_PKT_DATA_INBAND, 0);
197
198}
199
200/*
201 * This callback is invoked when we get a VSS message from the host.
202 * The host ensures that only one VSS transaction can be active at a time.
203 */
204
205void hv_vss_onchannelcallback(void *context)
206{
207 struct vmbus_channel *channel = context;
208 u32 recvlen;
209 u64 requestid;
210 struct hv_vss_msg *vss_msg;
211
212
213 struct icmsg_hdr *icmsghdrp;
214 struct icmsg_negotiate *negop = NULL;
215
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700216 if (vss_transaction.state > HVUTIL_READY) {
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700217 /*
218 * We will defer processing this callback once
219 * the current transaction is complete.
220 */
Vitaly Kuznetsov38c06c22015-04-11 18:07:43 -0700221 vss_transaction.vss_context = context;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700222 return;
223 }
Vitaly Kuznetsov38c06c22015-04-11 18:07:43 -0700224 vss_transaction.vss_context = NULL;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700225
226 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
227 &requestid);
228
229 if (recvlen > 0) {
230 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
231 sizeof(struct vmbuspipe_hdr)];
232
233 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
234 vmbus_prep_negotiate_resp(icmsghdrp, negop,
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700235 recv_buffer, UTIL_FW_VERSION,
236 VSS_VERSION);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700237 } else {
238 vss_msg = (struct hv_vss_msg *)&recv_buffer[
239 sizeof(struct vmbuspipe_hdr) +
240 sizeof(struct icmsg_hdr)];
241
242 /*
243 * Stash away this global state for completing the
244 * transaction; note transactions are serialized.
245 */
246
247 vss_transaction.recv_len = recvlen;
248 vss_transaction.recv_channel = channel;
249 vss_transaction.recv_req_id = requestid;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700250 vss_transaction.msg = (struct hv_vss_msg *)vss_msg;
251
252 switch (vss_msg->vss_hdr.operation) {
253 /*
254 * Initiate a "freeze/thaw"
255 * operation in the guest.
256 * We respond to the host once
257 * the operation is complete.
258 *
259 * We send the message to the
260 * user space daemon and the
261 * operation is performed in
262 * the daemon.
263 */
264 case VSS_OP_FREEZE:
265 case VSS_OP_THAW:
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700266 if (vss_transaction.state < HVUTIL_READY) {
267 /* Userspace is not registered yet */
268 vss_respond_to_host(HV_E_FAIL);
269 return;
270 }
271 vss_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700272 schedule_work(&vss_send_op_work);
Vitaly Kuznetsov64914202014-11-06 18:21:24 +0100273 schedule_delayed_work(&vss_timeout_work,
274 VSS_USERSPACE_TIMEOUT);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700275 return;
276
277 case VSS_OP_HOT_BACKUP:
278 vss_msg->vss_cf.flags =
279 VSS_HBU_NO_AUTO_RECOVERY;
280 vss_respond_to_host(0);
281 return;
282
283 case VSS_OP_GET_DM_INFO:
284 vss_msg->dm_info.flags = 0;
285 vss_respond_to_host(0);
286 return;
287
288 default:
289 vss_respond_to_host(0);
290 return;
291
292 }
293
294 }
295
296 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
297 | ICMSGHDRFLAG_RESPONSE;
298
299 vmbus_sendpacket(channel, recv_buffer,
300 recvlen, requestid,
301 VM_PKT_DATA_INBAND, 0);
302 }
303
304}
305
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700306static void vss_on_reset(void)
307{
308 if (cancel_delayed_work_sync(&vss_timeout_work))
309 vss_respond_to_host(HV_E_FAIL);
310 vss_transaction.state = HVUTIL_DEVICE_INIT;
311}
312
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700313int
314hv_vss_init(struct hv_util_service *srv)
315{
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700316 recv_buffer = srv->recv_buffer;
317
318 /*
319 * When this driver loads, the user level daemon that
320 * processes the host requests may not yet be running.
321 * Defer processing channel callbacks until the daemon
322 * has registered.
323 */
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700324 vss_transaction.state = HVUTIL_DEVICE_INIT;
325
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700326 hvt = hvutil_transport_init(vss_devname, CN_VSS_IDX, CN_VSS_VAL,
327 vss_on_msg, vss_on_reset);
328 if (!hvt)
329 return -EFAULT;
330
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700331 return 0;
332}
333
334void hv_vss_deinit(void)
335{
Vitaly Kuznetsov086a6f62015-04-11 18:07:48 -0700336 vss_transaction.state = HVUTIL_DEVICE_DYING;
Vitaly Kuznetsov64914202014-11-06 18:21:24 +0100337 cancel_delayed_work_sync(&vss_timeout_work);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700338 cancel_work_sync(&vss_send_op_work);
Vitaly Kuznetsov6472f802015-04-11 18:07:52 -0700339 hvutil_transport_destroy(hvt);
K. Y. Srinivasan96dd86f2013-03-15 12:30:06 -0700340}