2 * An implementation of host initiated guest snapshot.
5 * Copyright (C) 2013, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
27 #include "hyperv_vmbus.h"
31 #define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR)
33 #define VSS_USERSPACE_TIMEOUT (msecs_to_jiffies(10 * 1000))
36 * Global state maintained for transaction that is being processed. For a class
37 * of integration services, including the "VSS service", the specified protocol
38 * is a "request/response" protocol which means that there can only be single
39 * outstanding transaction from the host at any given point in time. We use
40 * this to simplify memory management in this driver - we cache and process
41 * only one message at a time.
43 * While the request/response protocol is guaranteed by the host, we further
44 * ensure this by serializing packet processing in this driver - we do not
45 * read additional packets from the VMBUs until the current packet is fully
50 int state
; /* hvutil_device_state */
51 int recv_len
; /* number of bytes received. */
52 struct vmbus_channel
*recv_channel
; /* chn we got the request */
53 u64 recv_req_id
; /* request ID. */
54 struct hv_vss_msg
*msg
; /* current message */
55 void *vss_context
; /* for the channel callback */
59 static void vss_respond_to_host(int error
);
61 static struct cb_id vss_id
= { CN_VSS_IDX
, CN_VSS_VAL
};
62 static const char vss_name
[] = "vss_kernel_module";
63 static __u8
*recv_buffer
;
65 static void vss_send_op(struct work_struct
*dummy
);
66 static void vss_timeout_func(struct work_struct
*dummy
);
68 static DECLARE_DELAYED_WORK(vss_timeout_work
, vss_timeout_func
);
69 static DECLARE_WORK(vss_send_op_work
, vss_send_op
);
72 * Callback when data is received from user mode.
75 static void vss_timeout_func(struct work_struct
*dummy
)
78 * Timeout waiting for userspace component to reply happened.
80 pr_warn("VSS: timeout waiting for daemon to reply\n");
81 vss_respond_to_host(HV_E_FAIL
);
83 /* Transaction is finished, reset the state. */
84 if (vss_transaction
.state
> HVUTIL_READY
)
85 vss_transaction
.state
= HVUTIL_READY
;
87 hv_poll_channel(vss_transaction
.vss_context
,
88 hv_vss_onchannelcallback
);
92 vss_cn_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
94 struct hv_vss_msg
*vss_msg
;
96 vss_msg
= (struct hv_vss_msg
*)msg
->data
;
99 * Don't process registration messages if we're in the middle of
100 * a transaction processing.
102 if (vss_transaction
.state
> HVUTIL_READY
&&
103 vss_msg
->vss_hdr
.operation
== VSS_OP_REGISTER
)
106 if (vss_transaction
.state
== HVUTIL_DEVICE_INIT
&&
107 vss_msg
->vss_hdr
.operation
== VSS_OP_REGISTER
) {
108 pr_info("VSS daemon registered\n");
109 vss_transaction
.state
= HVUTIL_READY
;
110 } else if (vss_transaction
.state
== HVUTIL_USERSPACE_REQ
) {
111 vss_transaction
.state
= HVUTIL_USERSPACE_RECV
;
112 if (cancel_delayed_work_sync(&vss_timeout_work
)) {
113 vss_respond_to_host(vss_msg
->error
);
114 /* Transaction is finished, reset the state. */
115 vss_transaction
.state
= HVUTIL_READY
;
116 hv_poll_channel(vss_transaction
.vss_context
,
117 hv_vss_onchannelcallback
);
120 /* This is a spurious call! */
121 pr_warn("VSS: Transaction not active\n");
127 static void vss_send_op(struct work_struct
*dummy
)
129 int op
= vss_transaction
.msg
->vss_hdr
.operation
;
132 struct hv_vss_msg
*vss_msg
;
134 /* The transaction state is wrong. */
135 if (vss_transaction
.state
!= HVUTIL_HOSTMSG_RECEIVED
)
138 msg
= kzalloc(sizeof(*msg
) + sizeof(*vss_msg
), GFP_ATOMIC
);
142 vss_msg
= (struct hv_vss_msg
*)msg
->data
;
144 msg
->id
.idx
= CN_VSS_IDX
;
145 msg
->id
.val
= CN_VSS_VAL
;
147 vss_msg
->vss_hdr
.operation
= op
;
148 msg
->len
= sizeof(struct hv_vss_msg
);
150 vss_transaction
.state
= HVUTIL_USERSPACE_REQ
;
151 rc
= cn_netlink_send(msg
, 0, 0, GFP_ATOMIC
);
153 pr_warn("VSS: failed to communicate to the daemon: %d\n", rc
);
154 if (cancel_delayed_work_sync(&vss_timeout_work
)) {
155 vss_respond_to_host(HV_E_FAIL
);
156 vss_transaction
.state
= HVUTIL_READY
;
166 * Send a response back to the host.
170 vss_respond_to_host(int error
)
172 struct icmsg_hdr
*icmsghdrp
;
174 struct vmbus_channel
*channel
;
178 * Copy the global state for completing the transaction. Note that
179 * only one transaction can be active at a time.
182 buf_len
= vss_transaction
.recv_len
;
183 channel
= vss_transaction
.recv_channel
;
184 req_id
= vss_transaction
.recv_req_id
;
186 icmsghdrp
= (struct icmsg_hdr
*)
187 &recv_buffer
[sizeof(struct vmbuspipe_hdr
)];
189 if (channel
->onchannel_callback
== NULL
)
191 * We have raced with util driver being unloaded;
196 icmsghdrp
->status
= error
;
198 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
200 vmbus_sendpacket(channel
, recv_buffer
, buf_len
, req_id
,
201 VM_PKT_DATA_INBAND
, 0);
206 * This callback is invoked when we get a VSS message from the host.
207 * The host ensures that only one VSS transaction can be active at a time.
210 void hv_vss_onchannelcallback(void *context
)
212 struct vmbus_channel
*channel
= context
;
215 struct hv_vss_msg
*vss_msg
;
218 struct icmsg_hdr
*icmsghdrp
;
219 struct icmsg_negotiate
*negop
= NULL
;
221 if (vss_transaction
.state
> HVUTIL_READY
) {
223 * We will defer processing this callback once
224 * the current transaction is complete.
226 vss_transaction
.vss_context
= context
;
229 vss_transaction
.vss_context
= NULL
;
231 vmbus_recvpacket(channel
, recv_buffer
, PAGE_SIZE
* 2, &recvlen
,
235 icmsghdrp
= (struct icmsg_hdr
*)&recv_buffer
[
236 sizeof(struct vmbuspipe_hdr
)];
238 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
239 vmbus_prep_negotiate_resp(icmsghdrp
, negop
,
240 recv_buffer
, UTIL_FW_VERSION
,
243 vss_msg
= (struct hv_vss_msg
*)&recv_buffer
[
244 sizeof(struct vmbuspipe_hdr
) +
245 sizeof(struct icmsg_hdr
)];
248 * Stash away this global state for completing the
249 * transaction; note transactions are serialized.
252 vss_transaction
.recv_len
= recvlen
;
253 vss_transaction
.recv_channel
= channel
;
254 vss_transaction
.recv_req_id
= requestid
;
255 vss_transaction
.msg
= (struct hv_vss_msg
*)vss_msg
;
257 switch (vss_msg
->vss_hdr
.operation
) {
259 * Initiate a "freeze/thaw"
260 * operation in the guest.
261 * We respond to the host once
262 * the operation is complete.
264 * We send the message to the
265 * user space daemon and the
266 * operation is performed in
271 if (vss_transaction
.state
< HVUTIL_READY
) {
272 /* Userspace is not registered yet */
273 vss_respond_to_host(HV_E_FAIL
);
276 vss_transaction
.state
= HVUTIL_HOSTMSG_RECEIVED
;
277 schedule_work(&vss_send_op_work
);
278 schedule_delayed_work(&vss_timeout_work
,
279 VSS_USERSPACE_TIMEOUT
);
282 case VSS_OP_HOT_BACKUP
:
283 vss_msg
->vss_cf
.flags
=
284 VSS_HBU_NO_AUTO_RECOVERY
;
285 vss_respond_to_host(0);
288 case VSS_OP_GET_DM_INFO
:
289 vss_msg
->dm_info
.flags
= 0;
290 vss_respond_to_host(0);
294 vss_respond_to_host(0);
301 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
302 | ICMSGHDRFLAG_RESPONSE
;
304 vmbus_sendpacket(channel
, recv_buffer
,
306 VM_PKT_DATA_INBAND
, 0);
312 hv_vss_init(struct hv_util_service
*srv
)
316 err
= cn_add_callback(&vss_id
, vss_name
, vss_cn_callback
);
319 recv_buffer
= srv
->recv_buffer
;
322 * When this driver loads, the user level daemon that
323 * processes the host requests may not yet be running.
324 * Defer processing channel callbacks until the daemon
327 vss_transaction
.state
= HVUTIL_DEVICE_INIT
;
332 void hv_vss_deinit(void)
334 vss_transaction
.state
= HVUTIL_DEVICE_DYING
;
335 cn_del_callback(&vss_id
);
336 cancel_delayed_work_sync(&vss_timeout_work
);
337 cancel_work_sync(&vss_send_op_work
);
This page took 0.038999 seconds and 6 git commands to generate.