Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An implementation of key value pair (KVP) functionality for Linux. |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2010, Novell, Inc. |
| 6 | * Author : K. Y. Srinivasan <ksrinivasan@novell.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 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/net.h> |
| 26 | #include <linux/nls.h> |
| 27 | #include <linux/connector.h> |
| 28 | #include <linux/workqueue.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 29 | #include <linux/hyperv.h> |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 30 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | /* |
| 34 | * Global state maintained for transaction that is being processed. |
| 35 | * Note that only one transaction can be active at any point in time. |
| 36 | * |
| 37 | * This state is set when we receive a request from the host; we |
| 38 | * cleanup this state when the transaction is completed - when we respond |
| 39 | * to the host with the key value. |
| 40 | */ |
| 41 | |
| 42 | static struct { |
| 43 | bool active; /* transaction status - active or not */ |
| 44 | int recv_len; /* number of bytes received. */ |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 45 | int index; /* current index */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 46 | struct vmbus_channel *recv_channel; /* chn we got the request */ |
| 47 | u64 recv_req_id; /* request ID. */ |
| 48 | } kvp_transaction; |
| 49 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 50 | static void kvp_send_key(struct work_struct *dummy); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 51 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 52 | #define TIMEOUT_FIRED 1 |
| 53 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 54 | static void kvp_respond_to_host(char *key, char *value, int error); |
| 55 | static void kvp_work_func(struct work_struct *dummy); |
| 56 | static void kvp_register(void); |
| 57 | |
| 58 | static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 59 | static DECLARE_WORK(kvp_sendkey_work, kvp_send_key); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 60 | |
| 61 | static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL }; |
| 62 | static const char kvp_name[] = "kvp_kernel_module"; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 63 | static u8 *recv_buffer; |
| 64 | /* |
| 65 | * Register the kernel component with the user-level daemon. |
| 66 | * As part of this registration, pass the LIC version number. |
| 67 | */ |
| 68 | |
| 69 | static void |
| 70 | kvp_register(void) |
| 71 | { |
| 72 | |
| 73 | struct cn_msg *msg; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 74 | struct hv_kvp_msg *kvp_msg; |
| 75 | char *version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 76 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 77 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 78 | |
| 79 | if (msg) { |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 80 | kvp_msg = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | e485ceac | 2012-03-10 15:32:08 -0800 | [diff] [blame^] | 81 | version = kvp_msg->body.kvp_register.version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 82 | msg->id.idx = CN_KVP_IDX; |
| 83 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 84 | |
| 85 | kvp_msg->kvp_hdr.operation = KVP_OP_REGISTER; |
| 86 | strcpy(version, HV_DRV_VERSION); |
| 87 | msg->len = sizeof(struct hv_kvp_msg); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 88 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
| 89 | kfree(msg); |
| 90 | } |
| 91 | } |
| 92 | static void |
| 93 | kvp_work_func(struct work_struct *dummy) |
| 94 | { |
| 95 | /* |
| 96 | * If the timer fires, the user-mode component has not responded; |
| 97 | * process the pending transaction. |
| 98 | */ |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 99 | kvp_respond_to_host("Unknown key", "Guest timed out", TIMEOUT_FIRED); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Callback when data is received from user mode. |
| 104 | */ |
| 105 | |
| 106 | static void |
| 107 | kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
| 108 | { |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 109 | struct hv_kvp_msg *message; |
| 110 | struct hv_kvp_msg_enumerate *data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 111 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 112 | message = (struct hv_kvp_msg *)msg->data; |
| 113 | if (message->kvp_hdr.operation == KVP_OP_REGISTER) { |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 114 | pr_info("KVP: user-mode registering done.\n"); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 115 | kvp_register(); |
| 116 | } |
| 117 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 118 | if (message->kvp_hdr.operation == KVP_OP_ENUMERATE) { |
| 119 | data = &message->body.kvp_enum_data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 120 | /* |
| 121 | * Complete the transaction by forwarding the key value |
| 122 | * to the host. But first, cancel the timeout. |
| 123 | */ |
| 124 | if (cancel_delayed_work_sync(&kvp_work)) |
K. Y. Srinivasan | e485ceac | 2012-03-10 15:32:08 -0800 | [diff] [blame^] | 125 | kvp_respond_to_host(data->data.key, |
| 126 | data->data.value, |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 127 | !strlen(data->data.key)); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 131 | static void |
| 132 | kvp_send_key(struct work_struct *dummy) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 133 | { |
| 134 | struct cn_msg *msg; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 135 | struct hv_kvp_msg *message; |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 136 | int index = kvp_transaction.index; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 137 | |
| 138 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC); |
| 139 | |
| 140 | if (msg) { |
| 141 | msg->id.idx = CN_KVP_IDX; |
| 142 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 143 | |
| 144 | message = (struct hv_kvp_msg *)msg->data; |
| 145 | message->kvp_hdr.operation = KVP_OP_ENUMERATE; |
| 146 | message->body.kvp_enum_data.index = index; |
| 147 | msg->len = sizeof(struct hv_kvp_msg); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 148 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
| 149 | kfree(msg); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 150 | } |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 151 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Send a response back to the host. |
| 156 | */ |
| 157 | |
| 158 | static void |
| 159 | kvp_respond_to_host(char *key, char *value, int error) |
| 160 | { |
| 161 | struct hv_kvp_msg *kvp_msg; |
| 162 | struct hv_kvp_msg_enumerate *kvp_data; |
| 163 | char *key_name; |
| 164 | struct icmsg_hdr *icmsghdrp; |
| 165 | int keylen, valuelen; |
| 166 | u32 buf_len; |
| 167 | struct vmbus_channel *channel; |
| 168 | u64 req_id; |
| 169 | |
| 170 | /* |
| 171 | * If a transaction is not active; log and return. |
| 172 | */ |
| 173 | |
| 174 | if (!kvp_transaction.active) { |
| 175 | /* |
| 176 | * This is a spurious call! |
| 177 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 178 | pr_warn("KVP: Transaction not active\n"); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | /* |
| 182 | * Copy the global state for completing the transaction. Note that |
| 183 | * only one transaction can be active at a time. |
| 184 | */ |
| 185 | |
| 186 | buf_len = kvp_transaction.recv_len; |
| 187 | channel = kvp_transaction.recv_channel; |
| 188 | req_id = kvp_transaction.recv_req_id; |
| 189 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 190 | kvp_transaction.active = false; |
| 191 | |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 192 | if (channel->onchannel_callback == NULL) |
| 193 | /* |
| 194 | * We have raced with util driver being unloaded; |
| 195 | * silently return. |
| 196 | */ |
| 197 | return; |
| 198 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 199 | icmsghdrp = (struct icmsg_hdr *) |
| 200 | &recv_buffer[sizeof(struct vmbuspipe_hdr)]; |
| 201 | kvp_msg = (struct hv_kvp_msg *) |
| 202 | &recv_buffer[sizeof(struct vmbuspipe_hdr) + |
| 203 | sizeof(struct icmsg_hdr)]; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 204 | kvp_data = &kvp_msg->body.kvp_enum_data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 205 | key_name = key; |
| 206 | |
| 207 | /* |
| 208 | * If the error parameter is set, terminate the host's enumeration. |
| 209 | */ |
| 210 | if (error) { |
| 211 | /* |
| 212 | * We don't support this index or the we have timedout; |
| 213 | * terminate the host-side iteration by returning an error. |
| 214 | */ |
| 215 | icmsghdrp->status = HV_E_FAIL; |
| 216 | goto response_done; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * The windows host expects the key/value pair to be encoded |
| 221 | * in utf16. |
| 222 | */ |
Alan Stern | 0720a06 | 2011-11-17 16:42:19 -0500 | [diff] [blame] | 223 | keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN, |
| 224 | (wchar_t *) kvp_data->data.key, |
| 225 | HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 226 | kvp_data->data.key_size = 2*(keylen + 1); /* utf16 encoding */ |
Alan Stern | 0720a06 | 2011-11-17 16:42:19 -0500 | [diff] [blame] | 227 | valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN, |
| 228 | (wchar_t *) kvp_data->data.value, |
| 229 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 230 | kvp_data->data.value_size = 2*(valuelen + 1); /* utf16 encoding */ |
| 231 | |
| 232 | kvp_data->data.value_type = REG_SZ; /* all our values are strings */ |
| 233 | icmsghdrp->status = HV_S_OK; |
| 234 | |
| 235 | response_done: |
| 236 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; |
| 237 | |
| 238 | vmbus_sendpacket(channel, recv_buffer, buf_len, req_id, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 239 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 240 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | * This callback is invoked when we get a KVP message from the host. |
| 245 | * The host ensures that only one KVP transaction can be active at a time. |
| 246 | * KVP implementation in Linux needs to forward the key to a user-mde |
| 247 | * component to retrive the corresponding value. Consequently, we cannot |
| 248 | * respond to the host in the conext of this callback. Since the host |
| 249 | * guarantees that at most only one transaction can be active at a time, |
| 250 | * we stash away the transaction state in a set of global variables. |
| 251 | */ |
| 252 | |
| 253 | void hv_kvp_onchannelcallback(void *context) |
| 254 | { |
| 255 | struct vmbus_channel *channel = context; |
| 256 | u32 recvlen; |
| 257 | u64 requestid; |
| 258 | |
| 259 | struct hv_kvp_msg *kvp_msg; |
| 260 | struct hv_kvp_msg_enumerate *kvp_data; |
| 261 | |
| 262 | struct icmsg_hdr *icmsghdrp; |
| 263 | struct icmsg_negotiate *negop = NULL; |
| 264 | |
| 265 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 266 | vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid); |
| 267 | |
| 268 | if (recvlen > 0) { |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 269 | icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ |
| 270 | sizeof(struct vmbuspipe_hdr)]; |
| 271 | |
| 272 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { |
Greg Kroah-Hartman | da0e963 | 2011-10-11 08:42:28 -0600 | [diff] [blame] | 273 | vmbus_prep_negotiate_resp(icmsghdrp, negop, recv_buffer); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 274 | } else { |
| 275 | kvp_msg = (struct hv_kvp_msg *)&recv_buffer[ |
| 276 | sizeof(struct vmbuspipe_hdr) + |
| 277 | sizeof(struct icmsg_hdr)]; |
| 278 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 279 | kvp_data = &kvp_msg->body.kvp_enum_data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * We only support the "get" operation on |
| 283 | * "KVP_POOL_AUTO" pool. |
| 284 | */ |
| 285 | |
| 286 | if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) || |
| 287 | (kvp_msg->kvp_hdr.operation != |
| 288 | KVP_OP_ENUMERATE)) { |
| 289 | icmsghdrp->status = HV_E_FAIL; |
| 290 | goto callback_done; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Stash away this global state for completing the |
| 295 | * transaction; note transactions are serialized. |
| 296 | */ |
| 297 | kvp_transaction.recv_len = recvlen; |
| 298 | kvp_transaction.recv_channel = channel; |
| 299 | kvp_transaction.recv_req_id = requestid; |
| 300 | kvp_transaction.active = true; |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 301 | kvp_transaction.index = kvp_data->index; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * Get the information from the |
| 305 | * user-mode component. |
| 306 | * component. This transaction will be |
| 307 | * completed when we get the value from |
| 308 | * the user-mode component. |
| 309 | * Set a timeout to deal with |
| 310 | * user-mode not responding. |
| 311 | */ |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 312 | schedule_work(&kvp_sendkey_work); |
| 313 | schedule_delayed_work(&kvp_work, 5*HZ); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 314 | |
| 315 | return; |
| 316 | |
| 317 | } |
| 318 | |
| 319 | callback_done: |
| 320 | |
| 321 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
| 322 | | ICMSGHDRFLAG_RESPONSE; |
| 323 | |
| 324 | vmbus_sendpacket(channel, recv_buffer, |
| 325 | recvlen, requestid, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 326 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | } |
| 330 | |
| 331 | int |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 332 | hv_kvp_init(struct hv_util_service *srv) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 333 | { |
| 334 | int err; |
| 335 | |
| 336 | err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback); |
| 337 | if (err) |
| 338 | return err; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 339 | recv_buffer = srv->recv_buffer; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | void hv_kvp_deinit(void) |
| 345 | { |
| 346 | cn_del_callback(&kvp_id); |
| 347 | cancel_delayed_work_sync(&kvp_work); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 348 | cancel_work_sync(&kvp_sendkey_work); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 349 | } |