blob: 14c62e2bc0c9d8a231fdb248cef38cca82b1c801 [file] [log] [blame]
Ky Srinivasan245ba562010-12-16 18:54:16 -07001/*
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 Janssenaf7a5b62011-03-29 13:58:49 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Ky Srinivasan245ba562010-12-16 18:54:16 -070024
25#include <linux/net.h>
26#include <linux/nls.h>
27#include <linux/connector.h>
28#include <linux/workqueue.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
Ky Srinivasan245ba562010-12-16 18:54:16 -070030
Vitaly Kuznetsov3647a832015-04-11 18:07:39 -070031#include "hyperv_vmbus.h"
Ky Srinivasan245ba562010-12-16 18:54:16 -070032
K. Y. Srinivasan67413352013-07-02 10:31:30 -070033/*
34 * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
35 */
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070036#define WS2008_SRV_MAJOR 1
37#define WS2008_SRV_MINOR 0
38#define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
39
K. Y. Srinivasan67413352013-07-02 10:31:30 -070040#define WIN7_SRV_MAJOR 3
41#define WIN7_SRV_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070042#define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
K. Y. Srinivasan67413352013-07-02 10:31:30 -070043
44#define WIN8_SRV_MAJOR 4
45#define WIN8_SRV_MINOR 0
K. Y. Srinivasan3a491602013-09-06 11:49:56 -070046#define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
Ky Srinivasan245ba562010-12-16 18:54:16 -070047
48/*
49 * Global state maintained for transaction that is being processed.
50 * Note that only one transaction can be active at any point in time.
51 *
52 * This state is set when we receive a request from the host; we
53 * cleanup this state when the transaction is completed - when we respond
54 * to the host with the key value.
55 */
56
57static struct {
58 bool active; /* transaction status - active or not */
59 int recv_len; /* number of bytes received. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070060 struct hv_kvp_msg *kvp_msg; /* current message */
Ky Srinivasan245ba562010-12-16 18:54:16 -070061 struct vmbus_channel *recv_channel; /* chn we got the request */
62 u64 recv_req_id; /* request ID. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070063 void *kvp_context; /* for the channel callback */
Ky Srinivasan245ba562010-12-16 18:54:16 -070064} kvp_transaction;
65
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070066/*
67 * Before we can accept KVP messages from the host, we need
68 * to handshake with the user level daemon. This state tracks
69 * if we are in the handshake phase.
70 */
71static bool in_hand_shake = true;
72
73/*
74 * This state maintains the version number registered by the daemon.
75 */
76static int dm_reg_value;
77
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070078static void kvp_send_key(struct work_struct *dummy);
Ky Srinivasan245ba562010-12-16 18:54:16 -070079
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -070080
K. Y. Srinivasan03db7722012-08-16 18:32:12 -070081static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
Vitaly Kuznetsov68c8b392015-04-11 18:07:44 -070082static void kvp_timeout_func(struct work_struct *dummy);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070083static void kvp_register(int);
Ky Srinivasan245ba562010-12-16 18:54:16 -070084
Vitaly Kuznetsov68c8b392015-04-11 18:07:44 -070085static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070086static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
Ky Srinivasan245ba562010-12-16 18:54:16 -070087
88static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
89static const char kvp_name[] = "kvp_kernel_module";
Ky Srinivasan245ba562010-12-16 18:54:16 -070090static u8 *recv_buffer;
91/*
92 * Register the kernel component with the user-level daemon.
93 * As part of this registration, pass the LIC version number.
Olaf Heringcfc25992013-05-29 11:29:07 +020094 * This number has no meaning, it satisfies the registration protocol.
Ky Srinivasan245ba562010-12-16 18:54:16 -070095 */
Olaf Heringcfc25992013-05-29 11:29:07 +020096#define HV_DRV_VERSION "3.1"
Ky Srinivasan245ba562010-12-16 18:54:16 -070097
98static void
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070099kvp_register(int reg_value)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700100{
101
102 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800103 struct hv_kvp_msg *kvp_msg;
104 char *version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700105
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800106 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700107
108 if (msg) {
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800109 kvp_msg = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800110 version = kvp_msg->body.kvp_register.version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700111 msg->id.idx = CN_KVP_IDX;
112 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800113
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700114 kvp_msg->kvp_hdr.operation = reg_value;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800115 strcpy(version, HV_DRV_VERSION);
116 msg->len = sizeof(struct hv_kvp_msg);
David Friesac8f7332014-01-15 22:29:19 -0600117 cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700118 kfree(msg);
119 }
120}
Vitaly Kuznetsov68c8b392015-04-11 18:07:44 -0700121
122static void kvp_timeout_func(struct work_struct *dummy)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700123{
124 /*
125 * If the timer fires, the user-mode component has not responded;
126 * process the pending transaction.
127 */
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700128 kvp_respond_to_host(NULL, HV_E_FAIL);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700129}
130
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700131static int kvp_handle_handshake(struct hv_kvp_msg *msg)
132{
133 int ret = 1;
134
135 switch (msg->kvp_hdr.operation) {
136 case KVP_OP_REGISTER:
137 dm_reg_value = KVP_OP_REGISTER;
138 pr_info("KVP: IP injection functionality not available\n");
139 pr_info("KVP: Upgrade the KVP daemon\n");
140 break;
141 case KVP_OP_REGISTER1:
142 dm_reg_value = KVP_OP_REGISTER1;
143 break;
144 default:
145 pr_info("KVP: incompatible daemon\n");
146 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
147 KVP_OP_REGISTER1, msg->kvp_hdr.operation);
148 ret = 0;
149 }
150
151 if (ret) {
152 /*
153 * We have a compatible daemon; complete the handshake.
154 */
155 pr_info("KVP: user-mode registering done.\n");
156 kvp_register(dm_reg_value);
157 kvp_transaction.active = false;
Vitaly Kuznetsov8efe78f2015-04-11 18:07:41 -0700158 hv_poll_channel(kvp_transaction.kvp_context,
159 hv_kvp_onchannelcallback);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700160 }
161 return ret;
162}
163
164
Ky Srinivasan245ba562010-12-16 18:54:16 -0700165/*
166 * Callback when data is received from user mode.
167 */
168
169static void
170kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
171{
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800172 struct hv_kvp_msg *message;
173 struct hv_kvp_msg_enumerate *data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700174 int error = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700175
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800176 message = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700177
178 /*
179 * If we are negotiating the version information
180 * with the daemon; handle that first.
181 */
182
183 if (in_hand_shake) {
184 if (kvp_handle_handshake(message))
185 in_hand_shake = false;
186 return;
187 }
188
189 /*
190 * Based on the version of the daemon, we propagate errors from the
191 * daemon differently.
192 */
193
194 data = &message->body.kvp_enum_data;
195
196 switch (dm_reg_value) {
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700197 case KVP_OP_REGISTER:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700198 /*
199 * Null string is used to pass back error condition.
200 */
201 if (data->data.key[0] == 0)
202 error = HV_S_CONT;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700203 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700204
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700205 case KVP_OP_REGISTER1:
Ky Srinivasan245ba562010-12-16 18:54:16 -0700206 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700207 * We use the message header information from
208 * the user level daemon to transmit errors.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700209 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700210 error = message->error;
211 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700212 }
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700213
214 /*
215 * Complete the transaction by forwarding the key value
216 * to the host. But first, cancel the timeout.
217 */
Vitaly Kuznetsov68c8b392015-04-11 18:07:44 -0700218 if (cancel_delayed_work_sync(&kvp_timeout_work))
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700219 kvp_respond_to_host(message, error);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700220}
221
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700222
223static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
224{
225 struct hv_kvp_msg *in = in_msg;
226 struct hv_kvp_ip_msg *out = out_msg;
227 int len;
228
229 switch (op) {
230 case KVP_OP_GET_IP_INFO:
231 /*
232 * Transform all parameters into utf16 encoding.
233 */
234 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
235 strlen((char *)in->body.kvp_ip_val.ip_addr),
236 UTF16_HOST_ENDIAN,
237 (wchar_t *)out->kvp_ip_val.ip_addr,
238 MAX_IP_ADDR_SIZE);
239 if (len < 0)
240 return len;
241
242 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
243 strlen((char *)in->body.kvp_ip_val.sub_net),
244 UTF16_HOST_ENDIAN,
245 (wchar_t *)out->kvp_ip_val.sub_net,
246 MAX_IP_ADDR_SIZE);
247 if (len < 0)
248 return len;
249
250 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
251 strlen((char *)in->body.kvp_ip_val.gate_way),
252 UTF16_HOST_ENDIAN,
253 (wchar_t *)out->kvp_ip_val.gate_way,
254 MAX_GATEWAY_SIZE);
255 if (len < 0)
256 return len;
257
258 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
259 strlen((char *)in->body.kvp_ip_val.dns_addr),
260 UTF16_HOST_ENDIAN,
261 (wchar_t *)out->kvp_ip_val.dns_addr,
262 MAX_IP_ADDR_SIZE);
263 if (len < 0)
264 return len;
265
266 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
267 strlen((char *)in->body.kvp_ip_val.adapter_id),
268 UTF16_HOST_ENDIAN,
269 (wchar_t *)out->kvp_ip_val.adapter_id,
270 MAX_IP_ADDR_SIZE);
271 if (len < 0)
272 return len;
273
274 out->kvp_ip_val.dhcp_enabled =
275 in->body.kvp_ip_val.dhcp_enabled;
K. Y. Srinivasana500e0e2012-09-04 17:54:13 -0700276 out->kvp_ip_val.addr_family =
277 in->body.kvp_ip_val.addr_family;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700278 }
279
280 return 0;
281}
282
283static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
284{
285 struct hv_kvp_ip_msg *in = in_msg;
286 struct hv_kvp_msg *out = out_msg;
287
288 switch (op) {
289 case KVP_OP_SET_IP_INFO:
290 /*
291 * Transform all parameters into utf8 encoding.
292 */
293 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
294 MAX_IP_ADDR_SIZE,
295 UTF16_LITTLE_ENDIAN,
296 (__u8 *)out->body.kvp_ip_val.ip_addr,
297 MAX_IP_ADDR_SIZE);
298
299 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
300 MAX_IP_ADDR_SIZE,
301 UTF16_LITTLE_ENDIAN,
302 (__u8 *)out->body.kvp_ip_val.sub_net,
303 MAX_IP_ADDR_SIZE);
304
305 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
306 MAX_GATEWAY_SIZE,
307 UTF16_LITTLE_ENDIAN,
308 (__u8 *)out->body.kvp_ip_val.gate_way,
309 MAX_GATEWAY_SIZE);
310
311 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
312 MAX_IP_ADDR_SIZE,
313 UTF16_LITTLE_ENDIAN,
314 (__u8 *)out->body.kvp_ip_val.dns_addr,
315 MAX_IP_ADDR_SIZE);
316
317 out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
318
319 default:
320 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
321 MAX_ADAPTER_ID_SIZE,
322 UTF16_LITTLE_ENDIAN,
323 (__u8 *)out->body.kvp_ip_val.adapter_id,
324 MAX_ADAPTER_ID_SIZE);
325
326 out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
327 }
328}
329
330
331
332
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700333static void
334kvp_send_key(struct work_struct *dummy)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700335{
336 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800337 struct hv_kvp_msg *message;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700338 struct hv_kvp_msg *in_msg;
339 __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
340 __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
341 __u32 val32;
342 __u64 val64;
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100343 int rc;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700344
345 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700346 if (!msg)
347 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700348
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700349 msg->id.idx = CN_KVP_IDX;
350 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800351
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700352 message = (struct hv_kvp_msg *)msg->data;
353 message->kvp_hdr.operation = operation;
354 message->kvp_hdr.pool = pool;
355 in_msg = kvp_transaction.kvp_msg;
356
357 /*
358 * The key/value strings sent from the host are encoded in
359 * in utf16; convert it to utf8 strings.
360 * The host assures us that the utf16 strings will not exceed
361 * the max lengths specified. We will however, reserve room
362 * for the string terminating character - in the utf16s_utf8s()
363 * function we limit the size of the buffer where the converted
364 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
365 * that the strings can be properly terminated!
366 */
367
368 switch (message->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700369 case KVP_OP_SET_IP_INFO:
370 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
371 break;
372 case KVP_OP_GET_IP_INFO:
373 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
374 break;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700375 case KVP_OP_SET:
376 switch (in_msg->body.kvp_set.data.value_type) {
377 case REG_SZ:
378 /*
379 * The value is a string - utf16 encoding.
380 */
381 message->body.kvp_set.data.value_size =
382 utf16s_to_utf8s(
383 (wchar_t *)in_msg->body.kvp_set.data.value,
384 in_msg->body.kvp_set.data.value_size,
385 UTF16_LITTLE_ENDIAN,
386 message->body.kvp_set.data.value,
387 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
388 break;
389
390 case REG_U32:
391 /*
392 * The value is a 32 bit scalar.
393 * We save this as a utf8 string.
394 */
395 val32 = in_msg->body.kvp_set.data.value_u32;
396 message->body.kvp_set.data.value_size =
397 sprintf(message->body.kvp_set.data.value,
398 "%d", val32) + 1;
399 break;
400
401 case REG_U64:
402 /*
403 * The value is a 64 bit scalar.
404 * We save this as a utf8 string.
405 */
406 val64 = in_msg->body.kvp_set.data.value_u64;
407 message->body.kvp_set.data.value_size =
408 sprintf(message->body.kvp_set.data.value,
409 "%llu", val64) + 1;
410 break;
411
412 }
413 case KVP_OP_GET:
414 message->body.kvp_set.data.key_size =
415 utf16s_to_utf8s(
416 (wchar_t *)in_msg->body.kvp_set.data.key,
417 in_msg->body.kvp_set.data.key_size,
418 UTF16_LITTLE_ENDIAN,
419 message->body.kvp_set.data.key,
420 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
421 break;
422
423 case KVP_OP_DELETE:
424 message->body.kvp_delete.key_size =
425 utf16s_to_utf8s(
426 (wchar_t *)in_msg->body.kvp_delete.key,
427 in_msg->body.kvp_delete.key_size,
428 UTF16_LITTLE_ENDIAN,
429 message->body.kvp_delete.key,
430 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
431 break;
432
433 case KVP_OP_ENUMERATE:
434 message->body.kvp_enum_data.index =
435 in_msg->body.kvp_enum_data.index;
436 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700437 }
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700438
439 msg->len = sizeof(struct hv_kvp_msg);
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100440 rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
441 if (rc) {
442 pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
Vitaly Kuznetsov68c8b392015-04-11 18:07:44 -0700443 if (cancel_delayed_work_sync(&kvp_timeout_work))
Vitaly Kuznetsov8d9560e2014-11-06 18:21:25 +0100444 kvp_respond_to_host(message, HV_E_FAIL);
445 }
446
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700447 kfree(msg);
448
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700449 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700450}
451
452/*
453 * Send a response back to the host.
454 */
455
456static void
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700457kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700458{
459 struct hv_kvp_msg *kvp_msg;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700460 struct hv_kvp_exchg_msg_value *kvp_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700461 char *key_name;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700462 char *value;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700463 struct icmsg_hdr *icmsghdrp;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700464 int keylen = 0;
465 int valuelen = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700466 u32 buf_len;
467 struct vmbus_channel *channel;
468 u64 req_id;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700469 int ret;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700470
471 /*
472 * If a transaction is not active; log and return.
473 */
474
475 if (!kvp_transaction.active) {
476 /*
477 * This is a spurious call!
478 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700479 pr_warn("KVP: Transaction not active\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700480 return;
481 }
482 /*
483 * Copy the global state for completing the transaction. Note that
484 * only one transaction can be active at a time.
485 */
486
487 buf_len = kvp_transaction.recv_len;
488 channel = kvp_transaction.recv_channel;
489 req_id = kvp_transaction.recv_req_id;
490
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -0700491 kvp_transaction.active = false;
492
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700493 icmsghdrp = (struct icmsg_hdr *)
494 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
495
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700496 if (channel->onchannel_callback == NULL)
497 /*
498 * We have raced with util driver being unloaded;
499 * silently return.
500 */
501 return;
502
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700503 icmsghdrp->status = error;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700504
505 /*
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700506 * If the error parameter is set, terminate the host's enumeration
507 * on this pool.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700508 */
509 if (error) {
510 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700511 * Something failed or we have timedout;
512 * terminate the current host-side iteration.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700513 */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700514 goto response_done;
515 }
516
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700517 kvp_msg = (struct hv_kvp_msg *)
518 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
519 sizeof(struct icmsg_hdr)];
520
521 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700522 case KVP_OP_GET_IP_INFO:
523 ret = process_ob_ipinfo(msg_to_host,
524 (struct hv_kvp_ip_msg *)kvp_msg,
525 KVP_OP_GET_IP_INFO);
526 if (ret < 0)
527 icmsghdrp->status = HV_E_FAIL;
528
529 goto response_done;
530 case KVP_OP_SET_IP_INFO:
531 goto response_done;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700532 case KVP_OP_GET:
533 kvp_data = &kvp_msg->body.kvp_get.data;
534 goto copy_value;
535
536 case KVP_OP_SET:
537 case KVP_OP_DELETE:
538 goto response_done;
539
540 default:
541 break;
542 }
543
544 kvp_data = &kvp_msg->body.kvp_enum_data.data;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700545 key_name = msg_to_host->body.kvp_enum_data.data.key;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700546
Ky Srinivasan245ba562010-12-16 18:54:16 -0700547 /*
548 * The windows host expects the key/value pair to be encoded
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700549 * in utf16. Ensure that the key/value size reported to the host
550 * will be less than or equal to the MAX size (including the
551 * terminating character).
Ky Srinivasan245ba562010-12-16 18:54:16 -0700552 */
Alan Stern0720a062011-11-17 16:42:19 -0500553 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700554 (wchar_t *) kvp_data->key,
555 (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
556 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700557
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700558copy_value:
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700559 value = msg_to_host->body.kvp_enum_data.data.value;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700560 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
561 (wchar_t *) kvp_data->value,
562 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
563 kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
564
565 /*
566 * If the utf8s to utf16s conversion failed; notify host
567 * of the error.
568 */
569 if ((keylen < 0) || (valuelen < 0))
570 icmsghdrp->status = HV_E_FAIL;
571
572 kvp_data->value_type = REG_SZ; /* all our values are strings */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700573
574response_done:
575 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
576
577 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800578 VM_PKT_DATA_INBAND, 0);
Vitaly Kuznetsov8efe78f2015-04-11 18:07:41 -0700579 hv_poll_channel(channel, hv_kvp_onchannelcallback);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700580}
581
582/*
583 * This callback is invoked when we get a KVP message from the host.
584 * The host ensures that only one KVP transaction can be active at a time.
585 * KVP implementation in Linux needs to forward the key to a user-mde
586 * component to retrive the corresponding value. Consequently, we cannot
587 * respond to the host in the conext of this callback. Since the host
588 * guarantees that at most only one transaction can be active at a time,
589 * we stash away the transaction state in a set of global variables.
590 */
591
592void hv_kvp_onchannelcallback(void *context)
593{
594 struct vmbus_channel *channel = context;
595 u32 recvlen;
596 u64 requestid;
597
598 struct hv_kvp_msg *kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700599
600 struct icmsg_hdr *icmsghdrp;
601 struct icmsg_negotiate *negop = NULL;
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700602 int util_fw_version;
603 int kvp_srv_version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700604
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700605 if (kvp_transaction.active) {
606 /*
607 * We will defer processing this callback once
608 * the current transaction is complete.
609 */
610 kvp_transaction.kvp_context = context;
611 return;
612 }
Vitaly Kuznetsov5fa97482015-04-11 18:07:40 -0700613 kvp_transaction.kvp_context = NULL;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700614
K. Y. Srinivasan9bd2d0d2014-07-07 16:34:25 -0700615 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700616 &requestid);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700617
618 if (recvlen > 0) {
Ky Srinivasan245ba562010-12-16 18:54:16 -0700619 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
620 sizeof(struct vmbuspipe_hdr)];
621
622 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700623 /*
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700624 * Based on the host, select appropriate
625 * framework and service versions we will
626 * negotiate.
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700627 */
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700628 switch (vmbus_proto_version) {
629 case (VERSION_WS2008):
630 util_fw_version = UTIL_WS2K8_FW_VERSION;
631 kvp_srv_version = WS2008_SRV_VERSION;
632 break;
633 case (VERSION_WIN7):
634 util_fw_version = UTIL_FW_VERSION;
635 kvp_srv_version = WIN7_SRV_VERSION;
636 break;
637 default:
638 util_fw_version = UTIL_FW_VERSION;
639 kvp_srv_version = WIN8_SRV_VERSION;
640 }
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700641 vmbus_prep_negotiate_resp(icmsghdrp, negop,
K. Y. Srinivasan3a491602013-09-06 11:49:56 -0700642 recv_buffer, util_fw_version,
643 kvp_srv_version);
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700644
Ky Srinivasan245ba562010-12-16 18:54:16 -0700645 } else {
646 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
647 sizeof(struct vmbuspipe_hdr) +
648 sizeof(struct icmsg_hdr)];
649
Ky Srinivasan245ba562010-12-16 18:54:16 -0700650 /*
651 * Stash away this global state for completing the
652 * transaction; note transactions are serialized.
653 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700654
Ky Srinivasan245ba562010-12-16 18:54:16 -0700655 kvp_transaction.recv_len = recvlen;
656 kvp_transaction.recv_channel = channel;
657 kvp_transaction.recv_req_id = requestid;
658 kvp_transaction.active = true;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700659 kvp_transaction.kvp_msg = kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700660
661 /*
662 * Get the information from the
663 * user-mode component.
664 * component. This transaction will be
665 * completed when we get the value from
666 * the user-mode component.
667 * Set a timeout to deal with
668 * user-mode not responding.
669 */
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700670 schedule_work(&kvp_sendkey_work);
Vitaly Kuznetsov68c8b392015-04-11 18:07:44 -0700671 schedule_delayed_work(&kvp_timeout_work, 5*HZ);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700672
673 return;
674
675 }
676
Ky Srinivasan245ba562010-12-16 18:54:16 -0700677 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
678 | ICMSGHDRFLAG_RESPONSE;
679
680 vmbus_sendpacket(channel, recv_buffer,
681 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800682 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700683 }
684
685}
686
687int
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700688hv_kvp_init(struct hv_util_service *srv)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700689{
690 int err;
691
692 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
693 if (err)
694 return err;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700695 recv_buffer = srv->recv_buffer;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700696
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700697 /*
698 * When this driver loads, the user level daemon that
699 * processes the host requests may not yet be running.
700 * Defer processing channel callbacks until the daemon
701 * has registered.
702 */
703 kvp_transaction.active = true;
704
Ky Srinivasan245ba562010-12-16 18:54:16 -0700705 return 0;
706}
707
708void hv_kvp_deinit(void)
709{
710 cn_del_callback(&kvp_id);
Vitaly Kuznetsov68c8b392015-04-11 18:07:44 -0700711 cancel_delayed_work_sync(&kvp_timeout_work);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700712 cancel_work_sync(&kvp_sendkey_work);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700713}