blob: d9060502b073ac5c6b21f119228e937a8b653cfe [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
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
42static struct {
43 bool active; /* transaction status - active or not */
44 int recv_len; /* number of bytes received. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070045 struct hv_kvp_msg *kvp_msg; /* current message */
Ky Srinivasan245ba562010-12-16 18:54:16 -070046 struct vmbus_channel *recv_channel; /* chn we got the request */
47 u64 recv_req_id; /* request ID. */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -070048 void *kvp_context; /* for the channel callback */
Ky Srinivasan245ba562010-12-16 18:54:16 -070049} kvp_transaction;
50
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070051/*
52 * Before we can accept KVP messages from the host, we need
53 * to handshake with the user level daemon. This state tracks
54 * if we are in the handshake phase.
55 */
56static bool in_hand_shake = true;
57
58/*
59 * This state maintains the version number registered by the daemon.
60 */
61static int dm_reg_value;
62
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070063static void kvp_send_key(struct work_struct *dummy);
Ky Srinivasan245ba562010-12-16 18:54:16 -070064
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -070065
K. Y. Srinivasan03db7722012-08-16 18:32:12 -070066static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
Ky Srinivasan245ba562010-12-16 18:54:16 -070067static void kvp_work_func(struct work_struct *dummy);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070068static void kvp_register(int);
Ky Srinivasan245ba562010-12-16 18:54:16 -070069
70static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070071static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
Ky Srinivasan245ba562010-12-16 18:54:16 -070072
73static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
74static const char kvp_name[] = "kvp_kernel_module";
Ky Srinivasan245ba562010-12-16 18:54:16 -070075static u8 *recv_buffer;
76/*
77 * Register the kernel component with the user-level daemon.
78 * As part of this registration, pass the LIC version number.
79 */
80
81static void
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070082kvp_register(int reg_value)
Ky Srinivasan245ba562010-12-16 18:54:16 -070083{
84
85 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080086 struct hv_kvp_msg *kvp_msg;
87 char *version;
Ky Srinivasan245ba562010-12-16 18:54:16 -070088
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080089 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -070090
91 if (msg) {
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080092 kvp_msg = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -080093 version = kvp_msg->body.kvp_register.version;
Ky Srinivasan245ba562010-12-16 18:54:16 -070094 msg->id.idx = CN_KVP_IDX;
95 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080096
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070097 kvp_msg->kvp_hdr.operation = reg_value;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080098 strcpy(version, HV_DRV_VERSION);
99 msg->len = sizeof(struct hv_kvp_msg);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700100 cn_netlink_send(msg, 0, GFP_ATOMIC);
101 kfree(msg);
102 }
103}
104static void
105kvp_work_func(struct work_struct *dummy)
106{
107 /*
108 * If the timer fires, the user-mode component has not responded;
109 * process the pending transaction.
110 */
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700111 kvp_respond_to_host(NULL, HV_E_FAIL);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700112}
113
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700114static int kvp_handle_handshake(struct hv_kvp_msg *msg)
115{
116 int ret = 1;
117
118 switch (msg->kvp_hdr.operation) {
119 case KVP_OP_REGISTER:
120 dm_reg_value = KVP_OP_REGISTER;
121 pr_info("KVP: IP injection functionality not available\n");
122 pr_info("KVP: Upgrade the KVP daemon\n");
123 break;
124 case KVP_OP_REGISTER1:
125 dm_reg_value = KVP_OP_REGISTER1;
126 break;
127 default:
128 pr_info("KVP: incompatible daemon\n");
129 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
130 KVP_OP_REGISTER1, msg->kvp_hdr.operation);
131 ret = 0;
132 }
133
134 if (ret) {
135 /*
136 * We have a compatible daemon; complete the handshake.
137 */
138 pr_info("KVP: user-mode registering done.\n");
139 kvp_register(dm_reg_value);
140 kvp_transaction.active = false;
141 if (kvp_transaction.kvp_context)
142 hv_kvp_onchannelcallback(kvp_transaction.kvp_context);
143 }
144 return ret;
145}
146
147
Ky Srinivasan245ba562010-12-16 18:54:16 -0700148/*
149 * Callback when data is received from user mode.
150 */
151
152static void
153kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
154{
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800155 struct hv_kvp_msg *message;
156 struct hv_kvp_msg_enumerate *data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700157 int error = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700158
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800159 message = (struct hv_kvp_msg *)msg->data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700160
161 /*
162 * If we are negotiating the version information
163 * with the daemon; handle that first.
164 */
165
166 if (in_hand_shake) {
167 if (kvp_handle_handshake(message))
168 in_hand_shake = false;
169 return;
170 }
171
172 /*
173 * Based on the version of the daemon, we propagate errors from the
174 * daemon differently.
175 */
176
177 data = &message->body.kvp_enum_data;
178
179 switch (dm_reg_value) {
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700180 case KVP_OP_REGISTER:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700181 /*
182 * Null string is used to pass back error condition.
183 */
184 if (data->data.key[0] == 0)
185 error = HV_S_CONT;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700186 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700187
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700188 case KVP_OP_REGISTER1:
Ky Srinivasan245ba562010-12-16 18:54:16 -0700189 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700190 * We use the message header information from
191 * the user level daemon to transmit errors.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700192 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700193 error = message->error;
194 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700195 }
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700196
197 /*
198 * Complete the transaction by forwarding the key value
199 * to the host. But first, cancel the timeout.
200 */
201 if (cancel_delayed_work_sync(&kvp_work))
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700202 kvp_respond_to_host(message, error);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700203}
204
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700205
206static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
207{
208 struct hv_kvp_msg *in = in_msg;
209 struct hv_kvp_ip_msg *out = out_msg;
210 int len;
211
212 switch (op) {
213 case KVP_OP_GET_IP_INFO:
214 /*
215 * Transform all parameters into utf16 encoding.
216 */
217 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
218 strlen((char *)in->body.kvp_ip_val.ip_addr),
219 UTF16_HOST_ENDIAN,
220 (wchar_t *)out->kvp_ip_val.ip_addr,
221 MAX_IP_ADDR_SIZE);
222 if (len < 0)
223 return len;
224
225 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
226 strlen((char *)in->body.kvp_ip_val.sub_net),
227 UTF16_HOST_ENDIAN,
228 (wchar_t *)out->kvp_ip_val.sub_net,
229 MAX_IP_ADDR_SIZE);
230 if (len < 0)
231 return len;
232
233 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
234 strlen((char *)in->body.kvp_ip_val.gate_way),
235 UTF16_HOST_ENDIAN,
236 (wchar_t *)out->kvp_ip_val.gate_way,
237 MAX_GATEWAY_SIZE);
238 if (len < 0)
239 return len;
240
241 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
242 strlen((char *)in->body.kvp_ip_val.dns_addr),
243 UTF16_HOST_ENDIAN,
244 (wchar_t *)out->kvp_ip_val.dns_addr,
245 MAX_IP_ADDR_SIZE);
246 if (len < 0)
247 return len;
248
249 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
250 strlen((char *)in->body.kvp_ip_val.adapter_id),
251 UTF16_HOST_ENDIAN,
252 (wchar_t *)out->kvp_ip_val.adapter_id,
253 MAX_IP_ADDR_SIZE);
254 if (len < 0)
255 return len;
256
257 out->kvp_ip_val.dhcp_enabled =
258 in->body.kvp_ip_val.dhcp_enabled;
259 }
260
261 return 0;
262}
263
264static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
265{
266 struct hv_kvp_ip_msg *in = in_msg;
267 struct hv_kvp_msg *out = out_msg;
268
269 switch (op) {
270 case KVP_OP_SET_IP_INFO:
271 /*
272 * Transform all parameters into utf8 encoding.
273 */
274 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
275 MAX_IP_ADDR_SIZE,
276 UTF16_LITTLE_ENDIAN,
277 (__u8 *)out->body.kvp_ip_val.ip_addr,
278 MAX_IP_ADDR_SIZE);
279
280 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
281 MAX_IP_ADDR_SIZE,
282 UTF16_LITTLE_ENDIAN,
283 (__u8 *)out->body.kvp_ip_val.sub_net,
284 MAX_IP_ADDR_SIZE);
285
286 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
287 MAX_GATEWAY_SIZE,
288 UTF16_LITTLE_ENDIAN,
289 (__u8 *)out->body.kvp_ip_val.gate_way,
290 MAX_GATEWAY_SIZE);
291
292 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
293 MAX_IP_ADDR_SIZE,
294 UTF16_LITTLE_ENDIAN,
295 (__u8 *)out->body.kvp_ip_val.dns_addr,
296 MAX_IP_ADDR_SIZE);
297
298 out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
299
300 default:
301 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
302 MAX_ADAPTER_ID_SIZE,
303 UTF16_LITTLE_ENDIAN,
304 (__u8 *)out->body.kvp_ip_val.adapter_id,
305 MAX_ADAPTER_ID_SIZE);
306
307 out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
308 }
309}
310
311
312
313
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700314static void
315kvp_send_key(struct work_struct *dummy)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700316{
317 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800318 struct hv_kvp_msg *message;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700319 struct hv_kvp_msg *in_msg;
320 __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
321 __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
322 __u32 val32;
323 __u64 val64;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700324
325 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700326 if (!msg)
327 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700328
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700329 msg->id.idx = CN_KVP_IDX;
330 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800331
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700332 message = (struct hv_kvp_msg *)msg->data;
333 message->kvp_hdr.operation = operation;
334 message->kvp_hdr.pool = pool;
335 in_msg = kvp_transaction.kvp_msg;
336
337 /*
338 * The key/value strings sent from the host are encoded in
339 * in utf16; convert it to utf8 strings.
340 * The host assures us that the utf16 strings will not exceed
341 * the max lengths specified. We will however, reserve room
342 * for the string terminating character - in the utf16s_utf8s()
343 * function we limit the size of the buffer where the converted
344 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
345 * that the strings can be properly terminated!
346 */
347
348 switch (message->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700349 case KVP_OP_SET_IP_INFO:
350 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
351 break;
352 case KVP_OP_GET_IP_INFO:
353 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
354 break;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700355 case KVP_OP_SET:
356 switch (in_msg->body.kvp_set.data.value_type) {
357 case REG_SZ:
358 /*
359 * The value is a string - utf16 encoding.
360 */
361 message->body.kvp_set.data.value_size =
362 utf16s_to_utf8s(
363 (wchar_t *)in_msg->body.kvp_set.data.value,
364 in_msg->body.kvp_set.data.value_size,
365 UTF16_LITTLE_ENDIAN,
366 message->body.kvp_set.data.value,
367 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
368 break;
369
370 case REG_U32:
371 /*
372 * The value is a 32 bit scalar.
373 * We save this as a utf8 string.
374 */
375 val32 = in_msg->body.kvp_set.data.value_u32;
376 message->body.kvp_set.data.value_size =
377 sprintf(message->body.kvp_set.data.value,
378 "%d", val32) + 1;
379 break;
380
381 case REG_U64:
382 /*
383 * The value is a 64 bit scalar.
384 * We save this as a utf8 string.
385 */
386 val64 = in_msg->body.kvp_set.data.value_u64;
387 message->body.kvp_set.data.value_size =
388 sprintf(message->body.kvp_set.data.value,
389 "%llu", val64) + 1;
390 break;
391
392 }
393 case KVP_OP_GET:
394 message->body.kvp_set.data.key_size =
395 utf16s_to_utf8s(
396 (wchar_t *)in_msg->body.kvp_set.data.key,
397 in_msg->body.kvp_set.data.key_size,
398 UTF16_LITTLE_ENDIAN,
399 message->body.kvp_set.data.key,
400 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
401 break;
402
403 case KVP_OP_DELETE:
404 message->body.kvp_delete.key_size =
405 utf16s_to_utf8s(
406 (wchar_t *)in_msg->body.kvp_delete.key,
407 in_msg->body.kvp_delete.key_size,
408 UTF16_LITTLE_ENDIAN,
409 message->body.kvp_delete.key,
410 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
411 break;
412
413 case KVP_OP_ENUMERATE:
414 message->body.kvp_enum_data.index =
415 in_msg->body.kvp_enum_data.index;
416 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700417 }
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700418
419 msg->len = sizeof(struct hv_kvp_msg);
420 cn_netlink_send(msg, 0, GFP_ATOMIC);
421 kfree(msg);
422
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700423 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700424}
425
426/*
427 * Send a response back to the host.
428 */
429
430static void
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700431kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700432{
433 struct hv_kvp_msg *kvp_msg;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700434 struct hv_kvp_exchg_msg_value *kvp_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700435 char *key_name;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700436 char *value;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700437 struct icmsg_hdr *icmsghdrp;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700438 int keylen = 0;
439 int valuelen = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700440 u32 buf_len;
441 struct vmbus_channel *channel;
442 u64 req_id;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700443 int ret;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700444
445 /*
446 * If a transaction is not active; log and return.
447 */
448
449 if (!kvp_transaction.active) {
450 /*
451 * This is a spurious call!
452 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700453 pr_warn("KVP: Transaction not active\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700454 return;
455 }
456 /*
457 * Copy the global state for completing the transaction. Note that
458 * only one transaction can be active at a time.
459 */
460
461 buf_len = kvp_transaction.recv_len;
462 channel = kvp_transaction.recv_channel;
463 req_id = kvp_transaction.recv_req_id;
464
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -0700465 kvp_transaction.active = false;
466
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700467 icmsghdrp = (struct icmsg_hdr *)
468 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
469
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700470 if (channel->onchannel_callback == NULL)
471 /*
472 * We have raced with util driver being unloaded;
473 * silently return.
474 */
475 return;
476
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700477 icmsghdrp->status = error;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700478
479 /*
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700480 * If the error parameter is set, terminate the host's enumeration
481 * on this pool.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700482 */
483 if (error) {
484 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700485 * Something failed or we have timedout;
486 * terminate the current host-side iteration.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700487 */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700488 goto response_done;
489 }
490
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700491 kvp_msg = (struct hv_kvp_msg *)
492 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
493 sizeof(struct icmsg_hdr)];
494
495 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700496 case KVP_OP_GET_IP_INFO:
497 ret = process_ob_ipinfo(msg_to_host,
498 (struct hv_kvp_ip_msg *)kvp_msg,
499 KVP_OP_GET_IP_INFO);
500 if (ret < 0)
501 icmsghdrp->status = HV_E_FAIL;
502
503 goto response_done;
504 case KVP_OP_SET_IP_INFO:
505 goto response_done;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700506 case KVP_OP_GET:
507 kvp_data = &kvp_msg->body.kvp_get.data;
508 goto copy_value;
509
510 case KVP_OP_SET:
511 case KVP_OP_DELETE:
512 goto response_done;
513
514 default:
515 break;
516 }
517
518 kvp_data = &kvp_msg->body.kvp_enum_data.data;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700519 key_name = msg_to_host->body.kvp_enum_data.data.key;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700520
Ky Srinivasan245ba562010-12-16 18:54:16 -0700521 /*
522 * The windows host expects the key/value pair to be encoded
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700523 * in utf16. Ensure that the key/value size reported to the host
524 * will be less than or equal to the MAX size (including the
525 * terminating character).
Ky Srinivasan245ba562010-12-16 18:54:16 -0700526 */
Alan Stern0720a062011-11-17 16:42:19 -0500527 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700528 (wchar_t *) kvp_data->key,
529 (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
530 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700531
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700532copy_value:
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700533 value = msg_to_host->body.kvp_enum_data.data.value;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700534 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
535 (wchar_t *) kvp_data->value,
536 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
537 kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
538
539 /*
540 * If the utf8s to utf16s conversion failed; notify host
541 * of the error.
542 */
543 if ((keylen < 0) || (valuelen < 0))
544 icmsghdrp->status = HV_E_FAIL;
545
546 kvp_data->value_type = REG_SZ; /* all our values are strings */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700547
548response_done:
549 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
550
551 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800552 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700553
Ky Srinivasan245ba562010-12-16 18:54:16 -0700554}
555
556/*
557 * This callback is invoked when we get a KVP message from the host.
558 * The host ensures that only one KVP transaction can be active at a time.
559 * KVP implementation in Linux needs to forward the key to a user-mde
560 * component to retrive the corresponding value. Consequently, we cannot
561 * respond to the host in the conext of this callback. Since the host
562 * guarantees that at most only one transaction can be active at a time,
563 * we stash away the transaction state in a set of global variables.
564 */
565
566void hv_kvp_onchannelcallback(void *context)
567{
568 struct vmbus_channel *channel = context;
569 u32 recvlen;
570 u64 requestid;
571
572 struct hv_kvp_msg *kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700573
574 struct icmsg_hdr *icmsghdrp;
575 struct icmsg_negotiate *negop = NULL;
576
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700577 if (kvp_transaction.active) {
578 /*
579 * We will defer processing this callback once
580 * the current transaction is complete.
581 */
582 kvp_transaction.kvp_context = context;
583 return;
584 }
Ky Srinivasan245ba562010-12-16 18:54:16 -0700585
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700586 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
587 &requestid);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700588
589 if (recvlen > 0) {
Ky Srinivasan245ba562010-12-16 18:54:16 -0700590 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
591 sizeof(struct vmbuspipe_hdr)];
592
593 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700594 vmbus_prep_negotiate_resp(icmsghdrp, negop,
595 recv_buffer, MAX_SRV_VER, MAX_SRV_VER);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700596 } else {
597 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
598 sizeof(struct vmbuspipe_hdr) +
599 sizeof(struct icmsg_hdr)];
600
Ky Srinivasan245ba562010-12-16 18:54:16 -0700601 /*
602 * Stash away this global state for completing the
603 * transaction; note transactions are serialized.
604 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700605
Ky Srinivasan245ba562010-12-16 18:54:16 -0700606 kvp_transaction.recv_len = recvlen;
607 kvp_transaction.recv_channel = channel;
608 kvp_transaction.recv_req_id = requestid;
609 kvp_transaction.active = true;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700610 kvp_transaction.kvp_msg = kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700611
612 /*
613 * Get the information from the
614 * user-mode component.
615 * component. This transaction will be
616 * completed when we get the value from
617 * the user-mode component.
618 * Set a timeout to deal with
619 * user-mode not responding.
620 */
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700621 schedule_work(&kvp_sendkey_work);
622 schedule_delayed_work(&kvp_work, 5*HZ);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700623
624 return;
625
626 }
627
Ky Srinivasan245ba562010-12-16 18:54:16 -0700628 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
629 | ICMSGHDRFLAG_RESPONSE;
630
631 vmbus_sendpacket(channel, recv_buffer,
632 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800633 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700634 }
635
636}
637
638int
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700639hv_kvp_init(struct hv_util_service *srv)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700640{
641 int err;
642
643 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
644 if (err)
645 return err;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700646 recv_buffer = srv->recv_buffer;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700647
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700648 /*
649 * When this driver loads, the user level daemon that
650 * processes the host requests may not yet be running.
651 * Defer processing channel callbacks until the daemon
652 * has registered.
653 */
654 kvp_transaction.active = true;
655
Ky Srinivasan245ba562010-12-16 18:54:16 -0700656 return 0;
657}
658
659void hv_kvp_deinit(void)
660{
661 cn_del_callback(&kvp_id);
662 cancel_delayed_work_sync(&kvp_work);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700663 cancel_work_sync(&kvp_sendkey_work);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700664}