K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 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 | |
Vitaly Kuznetsov | 3647a83 | 2015-04-11 18:07:39 -0700 | [diff] [blame] | 27 | #include "hyperv_vmbus.h" |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 28 | #include "hv_utils_transport.h" |
Vitaly Kuznetsov | 3647a83 | 2015-04-11 18:07:39 -0700 | [diff] [blame] | 29 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 30 | #define VSS_MAJOR 5 |
| 31 | #define VSS_MINOR 0 |
K. Y. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 32 | #define VSS_VERSION (VSS_MAJOR << 16 | VSS_MINOR) |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 33 | |
Vitaly Kuznetsov | 6491420 | 2014-11-06 18:21:24 +0100 | [diff] [blame] | 34 | #define VSS_USERSPACE_TIMEOUT (msecs_to_jiffies(10 * 1000)) |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 37 | * 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. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 43 | * |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 44 | * 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. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | static struct { |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 51 | int state; /* hvutil_device_state */ |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 52 | 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 Kuznetsov | 38c06c2 | 2015-04-11 18:07:43 -0700 | [diff] [blame] | 56 | void *vss_context; /* for the channel callback */ |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 57 | } vss_transaction; |
| 58 | |
| 59 | |
| 60 | static void vss_respond_to_host(int error); |
| 61 | |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 62 | static const char vss_devname[] = "vmbus/hv_vss"; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 63 | static __u8 *recv_buffer; |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 64 | static struct hvutil_transport *hvt; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 65 | |
| 66 | static void vss_send_op(struct work_struct *dummy); |
Vitaly Kuznetsov | 6491420 | 2014-11-06 18:21:24 +0100 | [diff] [blame] | 67 | static void vss_timeout_func(struct work_struct *dummy); |
| 68 | |
| 69 | static DECLARE_DELAYED_WORK(vss_timeout_work, vss_timeout_func); |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 70 | static DECLARE_WORK(vss_send_op_work, vss_send_op); |
| 71 | |
| 72 | /* |
| 73 | * Callback when data is received from user mode. |
| 74 | */ |
| 75 | |
Vitaly Kuznetsov | 6491420 | 2014-11-06 18:21:24 +0100 | [diff] [blame] | 76 | static 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 Kuznetsov | 38c06c2 | 2015-04-11 18:07:43 -0700 | [diff] [blame] | 83 | |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 84 | /* Transaction is finished, reset the state. */ |
| 85 | if (vss_transaction.state > HVUTIL_READY) |
| 86 | vss_transaction.state = HVUTIL_READY; |
| 87 | |
Vitaly Kuznetsov | 38c06c2 | 2015-04-11 18:07:43 -0700 | [diff] [blame] | 88 | hv_poll_channel(vss_transaction.vss_context, |
| 89 | hv_vss_onchannelcallback); |
Vitaly Kuznetsov | 6491420 | 2014-11-06 18:21:24 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 92 | static int vss_on_msg(void *msg, int len) |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 93 | { |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 94 | struct hv_vss_msg *vss_msg = (struct hv_vss_msg *)msg; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 95 | |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 96 | if (len != sizeof(*vss_msg)) |
| 97 | return -EINVAL; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 98 | |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 99 | /* |
| 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 Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 105 | return -EINVAL; |
Vitaly Kuznetsov | 38c06c2 | 2015-04-11 18:07:43 -0700 | [diff] [blame] | 106 | |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 107 | 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 Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 123 | return -EINVAL; |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 124 | } |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 125 | return 0; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
| 129 | static void vss_send_op(struct work_struct *dummy) |
| 130 | { |
| 131 | int op = vss_transaction.msg->vss_hdr.operation; |
Vitaly Kuznetsov | 8d9560e | 2014-11-06 18:21:25 +0100 | [diff] [blame] | 132 | int rc; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 133 | struct hv_vss_msg *vss_msg; |
| 134 | |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 135 | /* The transaction state is wrong. */ |
| 136 | if (vss_transaction.state != HVUTIL_HOSTMSG_RECEIVED) |
| 137 | return; |
| 138 | |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 139 | vss_msg = kzalloc(sizeof(*vss_msg), GFP_KERNEL); |
| 140 | if (!vss_msg) |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 141 | return; |
| 142 | |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 143 | vss_msg->vss_hdr.operation = op; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 144 | |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 145 | vss_transaction.state = HVUTIL_USERSPACE_REQ; |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 146 | rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg)); |
Vitaly Kuznetsov | 8d9560e | 2014-11-06 18:21:25 +0100 | [diff] [blame] | 147 | if (rc) { |
| 148 | pr_warn("VSS: failed to communicate to the daemon: %d\n", rc); |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 149 | if (cancel_delayed_work_sync(&vss_timeout_work)) { |
Vitaly Kuznetsov | 8d9560e | 2014-11-06 18:21:25 +0100 | [diff] [blame] | 150 | vss_respond_to_host(HV_E_FAIL); |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 151 | vss_transaction.state = HVUTIL_READY; |
| 152 | } |
Vitaly Kuznetsov | 8d9560e | 2014-11-06 18:21:25 +0100 | [diff] [blame] | 153 | } |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 154 | |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 155 | kfree(vss_msg); |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 156 | |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Send a response back to the host. |
| 162 | */ |
| 163 | |
| 164 | static void |
| 165 | vss_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. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 173 | * 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. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 180 | |
| 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 | |
| 205 | void 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 Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 216 | if (vss_transaction.state > HVUTIL_READY) { |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 217 | /* |
| 218 | * We will defer processing this callback once |
| 219 | * the current transaction is complete. |
| 220 | */ |
Vitaly Kuznetsov | 38c06c2 | 2015-04-11 18:07:43 -0700 | [diff] [blame] | 221 | vss_transaction.vss_context = context; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 222 | return; |
| 223 | } |
Vitaly Kuznetsov | 38c06c2 | 2015-04-11 18:07:43 -0700 | [diff] [blame] | 224 | vss_transaction.vss_context = NULL; |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 225 | |
| 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. Srinivasan | 3a49160 | 2013-09-06 11:49:56 -0700 | [diff] [blame] | 235 | recv_buffer, UTIL_FW_VERSION, |
| 236 | VSS_VERSION); |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 237 | } 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. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 250 | 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 Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 266 | 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. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 272 | schedule_work(&vss_send_op_work); |
Vitaly Kuznetsov | 6491420 | 2014-11-06 18:21:24 +0100 | [diff] [blame] | 273 | schedule_delayed_work(&vss_timeout_work, |
| 274 | VSS_USERSPACE_TIMEOUT); |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 275 | 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 Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 306 | static 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. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 313 | int |
| 314 | hv_vss_init(struct hv_util_service *srv) |
| 315 | { |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 316 | 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 Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 324 | vss_transaction.state = HVUTIL_DEVICE_INIT; |
| 325 | |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 326 | 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. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | void hv_vss_deinit(void) |
| 335 | { |
Vitaly Kuznetsov | 086a6f6 | 2015-04-11 18:07:48 -0700 | [diff] [blame] | 336 | vss_transaction.state = HVUTIL_DEVICE_DYING; |
Vitaly Kuznetsov | 6491420 | 2014-11-06 18:21:24 +0100 | [diff] [blame] | 337 | cancel_delayed_work_sync(&vss_timeout_work); |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 338 | cancel_work_sync(&vss_send_op_work); |
Vitaly Kuznetsov | 6472f80 | 2015-04-11 18:07:52 -0700 | [diff] [blame^] | 339 | hvutil_transport_destroy(hvt); |
K. Y. Srinivasan | 96dd86f | 2013-03-15 12:30:06 -0700 | [diff] [blame] | 340 | } |