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>
30 * Global state maintained for transaction that is being processed.
31 * Note that only one transaction can be active at any point in time.
33 * This state is set when we receive a request from the host; we
34 * cleanup this state when the transaction is completed - when we respond
35 * to the host with the key value.
39 bool active
; /* transaction status - active or not */
40 int recv_len
; /* number of bytes received. */
41 struct vmbus_channel
*recv_channel
; /* chn we got the request */
42 u64 recv_req_id
; /* request ID. */
43 struct hv_vss_msg
*msg
; /* current message */
47 static void vss_respond_to_host(int error
);
49 static struct cb_id vss_id
= { CN_VSS_IDX
, CN_VSS_VAL
};
50 static const char vss_name
[] = "vss_kernel_module";
51 static __u8
*recv_buffer
;
53 static void vss_send_op(struct work_struct
*dummy
);
54 static DECLARE_WORK(vss_send_op_work
, vss_send_op
);
57 * Callback when data is received from user mode.
61 vss_cn_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
63 struct hv_vss_msg
*vss_msg
;
65 vss_msg
= (struct hv_vss_msg
*)msg
->data
;
67 if (vss_msg
->vss_hdr
.operation
== VSS_OP_REGISTER
) {
68 pr_info("VSS daemon registered\n");
69 vss_transaction
.active
= false;
70 if (vss_transaction
.recv_channel
!= NULL
)
71 hv_vss_onchannelcallback(vss_transaction
.recv_channel
);
75 vss_respond_to_host(vss_msg
->error
);
79 static void vss_send_op(struct work_struct
*dummy
)
81 int op
= vss_transaction
.msg
->vss_hdr
.operation
;
83 struct hv_vss_msg
*vss_msg
;
85 msg
= kzalloc(sizeof(*msg
) + sizeof(*vss_msg
), GFP_ATOMIC
);
89 vss_msg
= (struct hv_vss_msg
*)msg
->data
;
91 msg
->id
.idx
= CN_VSS_IDX
;
92 msg
->id
.val
= CN_VSS_VAL
;
94 vss_msg
->vss_hdr
.operation
= op
;
95 msg
->len
= sizeof(struct hv_vss_msg
);
97 cn_netlink_send(msg
, 0, GFP_ATOMIC
);
104 * Send a response back to the host.
108 vss_respond_to_host(int error
)
110 struct icmsg_hdr
*icmsghdrp
;
112 struct vmbus_channel
*channel
;
116 * If a transaction is not active; log and return.
119 if (!vss_transaction
.active
) {
121 * This is a spurious call!
123 pr_warn("VSS: Transaction not active\n");
127 * Copy the global state for completing the transaction. Note that
128 * only one transaction can be active at a time.
131 buf_len
= vss_transaction
.recv_len
;
132 channel
= vss_transaction
.recv_channel
;
133 req_id
= vss_transaction
.recv_req_id
;
134 vss_transaction
.active
= false;
136 icmsghdrp
= (struct icmsg_hdr
*)
137 &recv_buffer
[sizeof(struct vmbuspipe_hdr
)];
139 if (channel
->onchannel_callback
== NULL
)
141 * We have raced with util driver being unloaded;
146 icmsghdrp
->status
= error
;
148 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
150 vmbus_sendpacket(channel
, recv_buffer
, buf_len
, req_id
,
151 VM_PKT_DATA_INBAND
, 0);
156 * This callback is invoked when we get a VSS message from the host.
157 * The host ensures that only one VSS transaction can be active at a time.
160 void hv_vss_onchannelcallback(void *context
)
162 struct vmbus_channel
*channel
= context
;
165 struct hv_vss_msg
*vss_msg
;
168 struct icmsg_hdr
*icmsghdrp
;
169 struct icmsg_negotiate
*negop
= NULL
;
171 if (vss_transaction
.active
) {
173 * We will defer processing this callback once
174 * the current transaction is complete.
176 vss_transaction
.recv_channel
= channel
;
180 vmbus_recvpacket(channel
, recv_buffer
, PAGE_SIZE
* 2, &recvlen
,
184 icmsghdrp
= (struct icmsg_hdr
*)&recv_buffer
[
185 sizeof(struct vmbuspipe_hdr
)];
187 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
188 vmbus_prep_negotiate_resp(icmsghdrp
, negop
,
189 recv_buffer
, MAX_SRV_VER
, MAX_SRV_VER
);
191 * We currently negotiate the highest number the
192 * host has presented. If this version is not
193 * atleast 5.0, reject.
195 negop
= (struct icmsg_negotiate
*)&recv_buffer
[
196 sizeof(struct vmbuspipe_hdr
) +
197 sizeof(struct icmsg_hdr
)];
199 if (negop
->icversion_data
[1].major
< 5)
200 negop
->icframe_vercnt
= 0;
202 vss_msg
= (struct hv_vss_msg
*)&recv_buffer
[
203 sizeof(struct vmbuspipe_hdr
) +
204 sizeof(struct icmsg_hdr
)];
207 * Stash away this global state for completing the
208 * transaction; note transactions are serialized.
211 vss_transaction
.recv_len
= recvlen
;
212 vss_transaction
.recv_channel
= channel
;
213 vss_transaction
.recv_req_id
= requestid
;
214 vss_transaction
.active
= true;
215 vss_transaction
.msg
= (struct hv_vss_msg
*)vss_msg
;
217 switch (vss_msg
->vss_hdr
.operation
) {
219 * Initiate a "freeze/thaw"
220 * operation in the guest.
221 * We respond to the host once
222 * the operation is complete.
224 * We send the message to the
225 * user space daemon and the
226 * operation is performed in
231 schedule_work(&vss_send_op_work
);
234 case VSS_OP_HOT_BACKUP
:
235 vss_msg
->vss_cf
.flags
=
236 VSS_HBU_NO_AUTO_RECOVERY
;
237 vss_respond_to_host(0);
240 case VSS_OP_GET_DM_INFO
:
241 vss_msg
->dm_info
.flags
= 0;
242 vss_respond_to_host(0);
246 vss_respond_to_host(0);
253 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
254 | ICMSGHDRFLAG_RESPONSE
;
256 vmbus_sendpacket(channel
, recv_buffer
,
258 VM_PKT_DATA_INBAND
, 0);
264 hv_vss_init(struct hv_util_service
*srv
)
268 err
= cn_add_callback(&vss_id
, vss_name
, vss_cn_callback
);
271 recv_buffer
= srv
->recv_buffer
;
274 * When this driver loads, the user level daemon that
275 * processes the host requests may not yet be running.
276 * Defer processing channel callbacks until the daemon
279 vss_transaction
.active
= true;
283 void hv_vss_deinit(void)
285 cn_del_callback(&vss_id
);
286 cancel_work_sync(&vss_send_op_work
);