blob: 53127209a40479866ccdaeee57c75fa26a8926a0 [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
Ky Srinivasan245ba562010-12-16 18:54:16 -070031
K. Y. Srinivasan67413352013-07-02 10:31:30 -070032/*
33 * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
34 */
35#define WIN7_SRV_MAJOR 3
36#define WIN7_SRV_MINOR 0
37#define WIN7_SRV_MAJOR_MINOR (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
38
39#define WIN8_SRV_MAJOR 4
40#define WIN8_SRV_MINOR 0
41#define WIN8_SRV_MAJOR_MINOR (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
Ky Srinivasan245ba562010-12-16 18:54:16 -070042
43/*
44 * Global state maintained for transaction that is being processed.
45 * Note that only one transaction can be active at any point in time.
46 *
47 * This state is set when we receive a request from the host; we
48 * cleanup this state when the transaction is completed - when we respond
49 * to the host with the key value.
50 */
51
52static struct {
53 bool active; /* transaction status - active or not */
54 int recv_len; /* number of bytes received. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070055 struct hv_kvp_msg *kvp_msg; /* current message */
Ky Srinivasan245ba562010-12-16 18:54:16 -070056 struct vmbus_channel *recv_channel; /* chn we got the request */
57 u64 recv_req_id; /* request ID. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070058 void *kvp_context; /* for the channel callback */
Ky Srinivasan245ba562010-12-16 18:54:16 -070059} kvp_transaction;
60
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070061/*
62 * Before we can accept KVP messages from the host, we need
63 * to handshake with the user level daemon. This state tracks
64 * if we are in the handshake phase.
65 */
66static bool in_hand_shake = true;
67
68/*
69 * This state maintains the version number registered by the daemon.
70 */
71static int dm_reg_value;
72
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070073static void kvp_send_key(struct work_struct *dummy);
Ky Srinivasan245ba562010-12-16 18:54:16 -070074
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -070075
K. Y. Srinivasan03db7722012-08-16 18:32:12 -070076static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
Ky Srinivasan245ba562010-12-16 18:54:16 -070077static void kvp_work_func(struct work_struct *dummy);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070078static void kvp_register(int);
Ky Srinivasan245ba562010-12-16 18:54:16 -070079
80static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070081static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
Ky Srinivasan245ba562010-12-16 18:54:16 -070082
83static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
84static const char kvp_name[] = "kvp_kernel_module";
Ky Srinivasan245ba562010-12-16 18:54:16 -070085static u8 *recv_buffer;
86/*
87 * Register the kernel component with the user-level daemon.
88 * As part of this registration, pass the LIC version number.
89 */
90
91static void
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070092kvp_register(int reg_value)
Ky Srinivasan245ba562010-12-16 18:54:16 -070093{
94
95 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080096 struct hv_kvp_msg *kvp_msg;
97 char *version;
Ky Srinivasan245ba562010-12-16 18:54:16 -070098
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080099 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700100
101 if (msg) {
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800102 kvp_msg = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800103 version = kvp_msg->body.kvp_register.version;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700104 msg->id.idx = CN_KVP_IDX;
105 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800106
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700107 kvp_msg->kvp_hdr.operation = reg_value;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800108 strcpy(version, HV_DRV_VERSION);
109 msg->len = sizeof(struct hv_kvp_msg);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700110 cn_netlink_send(msg, 0, GFP_ATOMIC);
111 kfree(msg);
112 }
113}
114static void
115kvp_work_func(struct work_struct *dummy)
116{
117 /*
118 * If the timer fires, the user-mode component has not responded;
119 * process the pending transaction.
120 */
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700121 kvp_respond_to_host(NULL, HV_E_FAIL);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700122}
123
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700124static int kvp_handle_handshake(struct hv_kvp_msg *msg)
125{
126 int ret = 1;
127
128 switch (msg->kvp_hdr.operation) {
129 case KVP_OP_REGISTER:
130 dm_reg_value = KVP_OP_REGISTER;
131 pr_info("KVP: IP injection functionality not available\n");
132 pr_info("KVP: Upgrade the KVP daemon\n");
133 break;
134 case KVP_OP_REGISTER1:
135 dm_reg_value = KVP_OP_REGISTER1;
136 break;
137 default:
138 pr_info("KVP: incompatible daemon\n");
139 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
140 KVP_OP_REGISTER1, msg->kvp_hdr.operation);
141 ret = 0;
142 }
143
144 if (ret) {
145 /*
146 * We have a compatible daemon; complete the handshake.
147 */
148 pr_info("KVP: user-mode registering done.\n");
149 kvp_register(dm_reg_value);
150 kvp_transaction.active = false;
151 if (kvp_transaction.kvp_context)
152 hv_kvp_onchannelcallback(kvp_transaction.kvp_context);
153 }
154 return ret;
155}
156
157
Ky Srinivasan245ba562010-12-16 18:54:16 -0700158/*
159 * Callback when data is received from user mode.
160 */
161
162static void
163kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
164{
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800165 struct hv_kvp_msg *message;
166 struct hv_kvp_msg_enumerate *data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700167 int error = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700168
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800169 message = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700170
171 /*
172 * If we are negotiating the version information
173 * with the daemon; handle that first.
174 */
175
176 if (in_hand_shake) {
177 if (kvp_handle_handshake(message))
178 in_hand_shake = false;
179 return;
180 }
181
182 /*
183 * Based on the version of the daemon, we propagate errors from the
184 * daemon differently.
185 */
186
187 data = &message->body.kvp_enum_data;
188
189 switch (dm_reg_value) {
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700190 case KVP_OP_REGISTER:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700191 /*
192 * Null string is used to pass back error condition.
193 */
194 if (data->data.key[0] == 0)
195 error = HV_S_CONT;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700196 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700197
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700198 case KVP_OP_REGISTER1:
Ky Srinivasan245ba562010-12-16 18:54:16 -0700199 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700200 * We use the message header information from
201 * the user level daemon to transmit errors.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700202 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700203 error = message->error;
204 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700205 }
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700206
207 /*
208 * Complete the transaction by forwarding the key value
209 * to the host. But first, cancel the timeout.
210 */
211 if (cancel_delayed_work_sync(&kvp_work))
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700212 kvp_respond_to_host(message, error);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700213}
214
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700215
216static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
217{
218 struct hv_kvp_msg *in = in_msg;
219 struct hv_kvp_ip_msg *out = out_msg;
220 int len;
221
222 switch (op) {
223 case KVP_OP_GET_IP_INFO:
224 /*
225 * Transform all parameters into utf16 encoding.
226 */
227 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
228 strlen((char *)in->body.kvp_ip_val.ip_addr),
229 UTF16_HOST_ENDIAN,
230 (wchar_t *)out->kvp_ip_val.ip_addr,
231 MAX_IP_ADDR_SIZE);
232 if (len < 0)
233 return len;
234
235 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
236 strlen((char *)in->body.kvp_ip_val.sub_net),
237 UTF16_HOST_ENDIAN,
238 (wchar_t *)out->kvp_ip_val.sub_net,
239 MAX_IP_ADDR_SIZE);
240 if (len < 0)
241 return len;
242
243 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
244 strlen((char *)in->body.kvp_ip_val.gate_way),
245 UTF16_HOST_ENDIAN,
246 (wchar_t *)out->kvp_ip_val.gate_way,
247 MAX_GATEWAY_SIZE);
248 if (len < 0)
249 return len;
250
251 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
252 strlen((char *)in->body.kvp_ip_val.dns_addr),
253 UTF16_HOST_ENDIAN,
254 (wchar_t *)out->kvp_ip_val.dns_addr,
255 MAX_IP_ADDR_SIZE);
256 if (len < 0)
257 return len;
258
259 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
260 strlen((char *)in->body.kvp_ip_val.adapter_id),
261 UTF16_HOST_ENDIAN,
262 (wchar_t *)out->kvp_ip_val.adapter_id,
263 MAX_IP_ADDR_SIZE);
264 if (len < 0)
265 return len;
266
267 out->kvp_ip_val.dhcp_enabled =
268 in->body.kvp_ip_val.dhcp_enabled;
K. Y. Srinivasana500e0e2012-09-04 17:54:13 -0700269 out->kvp_ip_val.addr_family =
270 in->body.kvp_ip_val.addr_family;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700271 }
272
273 return 0;
274}
275
276static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
277{
278 struct hv_kvp_ip_msg *in = in_msg;
279 struct hv_kvp_msg *out = out_msg;
280
281 switch (op) {
282 case KVP_OP_SET_IP_INFO:
283 /*
284 * Transform all parameters into utf8 encoding.
285 */
286 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
287 MAX_IP_ADDR_SIZE,
288 UTF16_LITTLE_ENDIAN,
289 (__u8 *)out->body.kvp_ip_val.ip_addr,
290 MAX_IP_ADDR_SIZE);
291
292 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
293 MAX_IP_ADDR_SIZE,
294 UTF16_LITTLE_ENDIAN,
295 (__u8 *)out->body.kvp_ip_val.sub_net,
296 MAX_IP_ADDR_SIZE);
297
298 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
299 MAX_GATEWAY_SIZE,
300 UTF16_LITTLE_ENDIAN,
301 (__u8 *)out->body.kvp_ip_val.gate_way,
302 MAX_GATEWAY_SIZE);
303
304 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
305 MAX_IP_ADDR_SIZE,
306 UTF16_LITTLE_ENDIAN,
307 (__u8 *)out->body.kvp_ip_val.dns_addr,
308 MAX_IP_ADDR_SIZE);
309
310 out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
311
312 default:
313 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
314 MAX_ADAPTER_ID_SIZE,
315 UTF16_LITTLE_ENDIAN,
316 (__u8 *)out->body.kvp_ip_val.adapter_id,
317 MAX_ADAPTER_ID_SIZE);
318
319 out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
320 }
321}
322
323
324
325
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700326static void
327kvp_send_key(struct work_struct *dummy)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700328{
329 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800330 struct hv_kvp_msg *message;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700331 struct hv_kvp_msg *in_msg;
332 __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
333 __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
334 __u32 val32;
335 __u64 val64;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700336
337 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700338 if (!msg)
339 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700340
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700341 msg->id.idx = CN_KVP_IDX;
342 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800343
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700344 message = (struct hv_kvp_msg *)msg->data;
345 message->kvp_hdr.operation = operation;
346 message->kvp_hdr.pool = pool;
347 in_msg = kvp_transaction.kvp_msg;
348
349 /*
350 * The key/value strings sent from the host are encoded in
351 * in utf16; convert it to utf8 strings.
352 * The host assures us that the utf16 strings will not exceed
353 * the max lengths specified. We will however, reserve room
354 * for the string terminating character - in the utf16s_utf8s()
355 * function we limit the size of the buffer where the converted
356 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
357 * that the strings can be properly terminated!
358 */
359
360 switch (message->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700361 case KVP_OP_SET_IP_INFO:
362 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
363 break;
364 case KVP_OP_GET_IP_INFO:
365 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
366 break;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700367 case KVP_OP_SET:
368 switch (in_msg->body.kvp_set.data.value_type) {
369 case REG_SZ:
370 /*
371 * The value is a string - utf16 encoding.
372 */
373 message->body.kvp_set.data.value_size =
374 utf16s_to_utf8s(
375 (wchar_t *)in_msg->body.kvp_set.data.value,
376 in_msg->body.kvp_set.data.value_size,
377 UTF16_LITTLE_ENDIAN,
378 message->body.kvp_set.data.value,
379 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
380 break;
381
382 case REG_U32:
383 /*
384 * The value is a 32 bit scalar.
385 * We save this as a utf8 string.
386 */
387 val32 = in_msg->body.kvp_set.data.value_u32;
388 message->body.kvp_set.data.value_size =
389 sprintf(message->body.kvp_set.data.value,
390 "%d", val32) + 1;
391 break;
392
393 case REG_U64:
394 /*
395 * The value is a 64 bit scalar.
396 * We save this as a utf8 string.
397 */
398 val64 = in_msg->body.kvp_set.data.value_u64;
399 message->body.kvp_set.data.value_size =
400 sprintf(message->body.kvp_set.data.value,
401 "%llu", val64) + 1;
402 break;
403
404 }
405 case KVP_OP_GET:
406 message->body.kvp_set.data.key_size =
407 utf16s_to_utf8s(
408 (wchar_t *)in_msg->body.kvp_set.data.key,
409 in_msg->body.kvp_set.data.key_size,
410 UTF16_LITTLE_ENDIAN,
411 message->body.kvp_set.data.key,
412 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
413 break;
414
415 case KVP_OP_DELETE:
416 message->body.kvp_delete.key_size =
417 utf16s_to_utf8s(
418 (wchar_t *)in_msg->body.kvp_delete.key,
419 in_msg->body.kvp_delete.key_size,
420 UTF16_LITTLE_ENDIAN,
421 message->body.kvp_delete.key,
422 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
423 break;
424
425 case KVP_OP_ENUMERATE:
426 message->body.kvp_enum_data.index =
427 in_msg->body.kvp_enum_data.index;
428 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700429 }
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700430
431 msg->len = sizeof(struct hv_kvp_msg);
432 cn_netlink_send(msg, 0, GFP_ATOMIC);
433 kfree(msg);
434
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700435 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700436}
437
438/*
439 * Send a response back to the host.
440 */
441
442static void
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700443kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700444{
445 struct hv_kvp_msg *kvp_msg;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700446 struct hv_kvp_exchg_msg_value *kvp_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700447 char *key_name;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700448 char *value;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700449 struct icmsg_hdr *icmsghdrp;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700450 int keylen = 0;
451 int valuelen = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700452 u32 buf_len;
453 struct vmbus_channel *channel;
454 u64 req_id;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700455 int ret;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700456
457 /*
458 * If a transaction is not active; log and return.
459 */
460
461 if (!kvp_transaction.active) {
462 /*
463 * This is a spurious call!
464 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700465 pr_warn("KVP: Transaction not active\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700466 return;
467 }
468 /*
469 * Copy the global state for completing the transaction. Note that
470 * only one transaction can be active at a time.
471 */
472
473 buf_len = kvp_transaction.recv_len;
474 channel = kvp_transaction.recv_channel;
475 req_id = kvp_transaction.recv_req_id;
476
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -0700477 kvp_transaction.active = false;
478
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700479 icmsghdrp = (struct icmsg_hdr *)
480 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
481
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700482 if (channel->onchannel_callback == NULL)
483 /*
484 * We have raced with util driver being unloaded;
485 * silently return.
486 */
487 return;
488
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700489 icmsghdrp->status = error;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700490
491 /*
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700492 * If the error parameter is set, terminate the host's enumeration
493 * on this pool.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700494 */
495 if (error) {
496 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700497 * Something failed or we have timedout;
498 * terminate the current host-side iteration.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700499 */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700500 goto response_done;
501 }
502
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700503 kvp_msg = (struct hv_kvp_msg *)
504 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
505 sizeof(struct icmsg_hdr)];
506
507 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700508 case KVP_OP_GET_IP_INFO:
509 ret = process_ob_ipinfo(msg_to_host,
510 (struct hv_kvp_ip_msg *)kvp_msg,
511 KVP_OP_GET_IP_INFO);
512 if (ret < 0)
513 icmsghdrp->status = HV_E_FAIL;
514
515 goto response_done;
516 case KVP_OP_SET_IP_INFO:
517 goto response_done;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700518 case KVP_OP_GET:
519 kvp_data = &kvp_msg->body.kvp_get.data;
520 goto copy_value;
521
522 case KVP_OP_SET:
523 case KVP_OP_DELETE:
524 goto response_done;
525
526 default:
527 break;
528 }
529
530 kvp_data = &kvp_msg->body.kvp_enum_data.data;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700531 key_name = msg_to_host->body.kvp_enum_data.data.key;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700532
Ky Srinivasan245ba562010-12-16 18:54:16 -0700533 /*
534 * The windows host expects the key/value pair to be encoded
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700535 * in utf16. Ensure that the key/value size reported to the host
536 * will be less than or equal to the MAX size (including the
537 * terminating character).
Ky Srinivasan245ba562010-12-16 18:54:16 -0700538 */
Alan Stern0720a062011-11-17 16:42:19 -0500539 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700540 (wchar_t *) kvp_data->key,
541 (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
542 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700543
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700544copy_value:
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700545 value = msg_to_host->body.kvp_enum_data.data.value;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700546 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
547 (wchar_t *) kvp_data->value,
548 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
549 kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
550
551 /*
552 * If the utf8s to utf16s conversion failed; notify host
553 * of the error.
554 */
555 if ((keylen < 0) || (valuelen < 0))
556 icmsghdrp->status = HV_E_FAIL;
557
558 kvp_data->value_type = REG_SZ; /* all our values are strings */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700559
560response_done:
561 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
562
563 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800564 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700565
Ky Srinivasan245ba562010-12-16 18:54:16 -0700566}
567
568/*
569 * This callback is invoked when we get a KVP message from the host.
570 * The host ensures that only one KVP transaction can be active at a time.
571 * KVP implementation in Linux needs to forward the key to a user-mde
572 * component to retrive the corresponding value. Consequently, we cannot
573 * respond to the host in the conext of this callback. Since the host
574 * guarantees that at most only one transaction can be active at a time,
575 * we stash away the transaction state in a set of global variables.
576 */
577
578void hv_kvp_onchannelcallback(void *context)
579{
580 struct vmbus_channel *channel = context;
581 u32 recvlen;
582 u64 requestid;
583
584 struct hv_kvp_msg *kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700585
586 struct icmsg_hdr *icmsghdrp;
587 struct icmsg_negotiate *negop = NULL;
588
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700589 if (kvp_transaction.active) {
590 /*
591 * We will defer processing this callback once
592 * the current transaction is complete.
593 */
594 kvp_transaction.kvp_context = context;
595 return;
596 }
Ky Srinivasan245ba562010-12-16 18:54:16 -0700597
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700598 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
599 &requestid);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700600
601 if (recvlen > 0) {
Ky Srinivasan245ba562010-12-16 18:54:16 -0700602 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
603 sizeof(struct vmbuspipe_hdr)];
604
605 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700606 /*
607 * We start with win8 version and if the host cannot
608 * support that we use the previous version.
609 */
610 if (vmbus_prep_negotiate_resp(icmsghdrp, negop,
611 recv_buffer, UTIL_FW_MAJOR_MINOR,
612 WIN8_SRV_MAJOR_MINOR))
613 goto done;
614
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700615 vmbus_prep_negotiate_resp(icmsghdrp, negop,
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700616 recv_buffer, UTIL_FW_MAJOR_MINOR,
617 WIN7_SRV_MAJOR_MINOR);
618
Ky Srinivasan245ba562010-12-16 18:54:16 -0700619 } else {
620 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
621 sizeof(struct vmbuspipe_hdr) +
622 sizeof(struct icmsg_hdr)];
623
Ky Srinivasan245ba562010-12-16 18:54:16 -0700624 /*
625 * Stash away this global state for completing the
626 * transaction; note transactions are serialized.
627 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700628
Ky Srinivasan245ba562010-12-16 18:54:16 -0700629 kvp_transaction.recv_len = recvlen;
630 kvp_transaction.recv_channel = channel;
631 kvp_transaction.recv_req_id = requestid;
632 kvp_transaction.active = true;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700633 kvp_transaction.kvp_msg = kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700634
635 /*
636 * Get the information from the
637 * user-mode component.
638 * component. This transaction will be
639 * completed when we get the value from
640 * the user-mode component.
641 * Set a timeout to deal with
642 * user-mode not responding.
643 */
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700644 schedule_work(&kvp_sendkey_work);
645 schedule_delayed_work(&kvp_work, 5*HZ);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700646
647 return;
648
649 }
K. Y. Srinivasan67413352013-07-02 10:31:30 -0700650done:
Ky Srinivasan245ba562010-12-16 18:54:16 -0700651
Ky Srinivasan245ba562010-12-16 18:54:16 -0700652 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
653 | ICMSGHDRFLAG_RESPONSE;
654
655 vmbus_sendpacket(channel, recv_buffer,
656 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800657 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700658 }
659
660}
661
662int
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700663hv_kvp_init(struct hv_util_service *srv)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700664{
665 int err;
666
667 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
668 if (err)
669 return err;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700670 recv_buffer = srv->recv_buffer;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700671
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700672 /*
673 * When this driver loads, the user level daemon that
674 * processes the host requests may not yet be running.
675 * Defer processing channel callbacks until the daemon
676 * has registered.
677 */
678 kvp_transaction.active = true;
679
Ky Srinivasan245ba562010-12-16 18:54:16 -0700680 return 0;
681}
682
683void hv_kvp_deinit(void)
684{
685 cn_del_callback(&kvp_id);
686 cancel_delayed_work_sync(&kvp_work);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700687 cancel_work_sync(&kvp_sendkey_work);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700688}