blob: ed50e9e83c61a8ec8690c757876a0300096f59d4 [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. Srinivasan26403352012-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. Srinivasan26403352012-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. Srinivasan26403352012-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. Srinivasan26403352012-02-02 16:56:50 -080096
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070097 kvp_msg->kvp_hdr.operation = reg_value;
K. Y. Srinivasan26403352012-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. Srinivasan26403352012-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. Srinivasan26403352012-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;
K. Y. Srinivasana500e0e2012-09-04 17:54:13 -0700259 out->kvp_ip_val.addr_family =
260 in->body.kvp_ip_val.addr_family;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700261 }
262
263 return 0;
264}
265
266static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
267{
268 struct hv_kvp_ip_msg *in = in_msg;
269 struct hv_kvp_msg *out = out_msg;
270
271 switch (op) {
272 case KVP_OP_SET_IP_INFO:
273 /*
274 * Transform all parameters into utf8 encoding.
275 */
276 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
277 MAX_IP_ADDR_SIZE,
278 UTF16_LITTLE_ENDIAN,
279 (__u8 *)out->body.kvp_ip_val.ip_addr,
280 MAX_IP_ADDR_SIZE);
281
282 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
283 MAX_IP_ADDR_SIZE,
284 UTF16_LITTLE_ENDIAN,
285 (__u8 *)out->body.kvp_ip_val.sub_net,
286 MAX_IP_ADDR_SIZE);
287
288 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
289 MAX_GATEWAY_SIZE,
290 UTF16_LITTLE_ENDIAN,
291 (__u8 *)out->body.kvp_ip_val.gate_way,
292 MAX_GATEWAY_SIZE);
293
294 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
295 MAX_IP_ADDR_SIZE,
296 UTF16_LITTLE_ENDIAN,
297 (__u8 *)out->body.kvp_ip_val.dns_addr,
298 MAX_IP_ADDR_SIZE);
299
300 out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
301
302 default:
303 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
304 MAX_ADAPTER_ID_SIZE,
305 UTF16_LITTLE_ENDIAN,
306 (__u8 *)out->body.kvp_ip_val.adapter_id,
307 MAX_ADAPTER_ID_SIZE);
308
309 out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
310 }
311}
312
313
314
315
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700316static void
317kvp_send_key(struct work_struct *dummy)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700318{
319 struct cn_msg *msg;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800320 struct hv_kvp_msg *message;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700321 struct hv_kvp_msg *in_msg;
322 __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
323 __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
324 __u32 val32;
325 __u64 val64;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700326
327 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700328 if (!msg)
329 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700330
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700331 msg->id.idx = CN_KVP_IDX;
332 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800333
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700334 message = (struct hv_kvp_msg *)msg->data;
335 message->kvp_hdr.operation = operation;
336 message->kvp_hdr.pool = pool;
337 in_msg = kvp_transaction.kvp_msg;
338
339 /*
340 * The key/value strings sent from the host are encoded in
341 * in utf16; convert it to utf8 strings.
342 * The host assures us that the utf16 strings will not exceed
343 * the max lengths specified. We will however, reserve room
344 * for the string terminating character - in the utf16s_utf8s()
345 * function we limit the size of the buffer where the converted
346 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
347 * that the strings can be properly terminated!
348 */
349
350 switch (message->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700351 case KVP_OP_SET_IP_INFO:
352 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
353 break;
354 case KVP_OP_GET_IP_INFO:
355 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
356 break;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700357 case KVP_OP_SET:
358 switch (in_msg->body.kvp_set.data.value_type) {
359 case REG_SZ:
360 /*
361 * The value is a string - utf16 encoding.
362 */
363 message->body.kvp_set.data.value_size =
364 utf16s_to_utf8s(
365 (wchar_t *)in_msg->body.kvp_set.data.value,
366 in_msg->body.kvp_set.data.value_size,
367 UTF16_LITTLE_ENDIAN,
368 message->body.kvp_set.data.value,
369 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
370 break;
371
372 case REG_U32:
373 /*
374 * The value is a 32 bit scalar.
375 * We save this as a utf8 string.
376 */
377 val32 = in_msg->body.kvp_set.data.value_u32;
378 message->body.kvp_set.data.value_size =
379 sprintf(message->body.kvp_set.data.value,
380 "%d", val32) + 1;
381 break;
382
383 case REG_U64:
384 /*
385 * The value is a 64 bit scalar.
386 * We save this as a utf8 string.
387 */
388 val64 = in_msg->body.kvp_set.data.value_u64;
389 message->body.kvp_set.data.value_size =
390 sprintf(message->body.kvp_set.data.value,
391 "%llu", val64) + 1;
392 break;
393
394 }
395 case KVP_OP_GET:
396 message->body.kvp_set.data.key_size =
397 utf16s_to_utf8s(
398 (wchar_t *)in_msg->body.kvp_set.data.key,
399 in_msg->body.kvp_set.data.key_size,
400 UTF16_LITTLE_ENDIAN,
401 message->body.kvp_set.data.key,
402 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
403 break;
404
405 case KVP_OP_DELETE:
406 message->body.kvp_delete.key_size =
407 utf16s_to_utf8s(
408 (wchar_t *)in_msg->body.kvp_delete.key,
409 in_msg->body.kvp_delete.key_size,
410 UTF16_LITTLE_ENDIAN,
411 message->body.kvp_delete.key,
412 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
413 break;
414
415 case KVP_OP_ENUMERATE:
416 message->body.kvp_enum_data.index =
417 in_msg->body.kvp_enum_data.index;
418 break;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700419 }
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700420
421 msg->len = sizeof(struct hv_kvp_msg);
422 cn_netlink_send(msg, 0, GFP_ATOMIC);
423 kfree(msg);
424
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700425 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700426}
427
428/*
429 * Send a response back to the host.
430 */
431
432static void
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700433kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700434{
435 struct hv_kvp_msg *kvp_msg;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700436 struct hv_kvp_exchg_msg_value *kvp_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700437 char *key_name;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700438 char *value;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700439 struct icmsg_hdr *icmsghdrp;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700440 int keylen = 0;
441 int valuelen = 0;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700442 u32 buf_len;
443 struct vmbus_channel *channel;
444 u64 req_id;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700445 int ret;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700446
447 /*
448 * If a transaction is not active; log and return.
449 */
450
451 if (!kvp_transaction.active) {
452 /*
453 * This is a spurious call!
454 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700455 pr_warn("KVP: Transaction not active\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700456 return;
457 }
458 /*
459 * Copy the global state for completing the transaction. Note that
460 * only one transaction can be active at a time.
461 */
462
463 buf_len = kvp_transaction.recv_len;
464 channel = kvp_transaction.recv_channel;
465 req_id = kvp_transaction.recv_req_id;
466
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -0700467 kvp_transaction.active = false;
468
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700469 icmsghdrp = (struct icmsg_hdr *)
470 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
471
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700472 if (channel->onchannel_callback == NULL)
473 /*
474 * We have raced with util driver being unloaded;
475 * silently return.
476 */
477 return;
478
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700479 icmsghdrp->status = error;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700480
481 /*
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700482 * If the error parameter is set, terminate the host's enumeration
483 * on this pool.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700484 */
485 if (error) {
486 /*
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700487 * Something failed or we have timedout;
488 * terminate the current host-side iteration.
Ky Srinivasan245ba562010-12-16 18:54:16 -0700489 */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700490 goto response_done;
491 }
492
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700493 kvp_msg = (struct hv_kvp_msg *)
494 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
495 sizeof(struct icmsg_hdr)];
496
497 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700498 case KVP_OP_GET_IP_INFO:
499 ret = process_ob_ipinfo(msg_to_host,
500 (struct hv_kvp_ip_msg *)kvp_msg,
501 KVP_OP_GET_IP_INFO);
502 if (ret < 0)
503 icmsghdrp->status = HV_E_FAIL;
504
505 goto response_done;
506 case KVP_OP_SET_IP_INFO:
507 goto response_done;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700508 case KVP_OP_GET:
509 kvp_data = &kvp_msg->body.kvp_get.data;
510 goto copy_value;
511
512 case KVP_OP_SET:
513 case KVP_OP_DELETE:
514 goto response_done;
515
516 default:
517 break;
518 }
519
520 kvp_data = &kvp_msg->body.kvp_enum_data.data;
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700521 key_name = msg_to_host->body.kvp_enum_data.data.key;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700522
Ky Srinivasan245ba562010-12-16 18:54:16 -0700523 /*
524 * The windows host expects the key/value pair to be encoded
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700525 * in utf16. Ensure that the key/value size reported to the host
526 * will be less than or equal to the MAX size (including the
527 * terminating character).
Ky Srinivasan245ba562010-12-16 18:54:16 -0700528 */
Alan Stern0720a062011-11-17 16:42:19 -0500529 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700530 (wchar_t *) kvp_data->key,
531 (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
532 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700533
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700534copy_value:
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700535 value = msg_to_host->body.kvp_enum_data.data.value;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700536 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
537 (wchar_t *) kvp_data->value,
538 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
539 kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
540
541 /*
542 * If the utf8s to utf16s conversion failed; notify host
543 * of the error.
544 */
545 if ((keylen < 0) || (valuelen < 0))
546 icmsghdrp->status = HV_E_FAIL;
547
548 kvp_data->value_type = REG_SZ; /* all our values are strings */
Ky Srinivasan245ba562010-12-16 18:54:16 -0700549
550response_done:
551 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
552
553 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800554 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700555
Ky Srinivasan245ba562010-12-16 18:54:16 -0700556}
557
558/*
559 * This callback is invoked when we get a KVP message from the host.
560 * The host ensures that only one KVP transaction can be active at a time.
561 * KVP implementation in Linux needs to forward the key to a user-mde
562 * component to retrive the corresponding value. Consequently, we cannot
563 * respond to the host in the conext of this callback. Since the host
564 * guarantees that at most only one transaction can be active at a time,
565 * we stash away the transaction state in a set of global variables.
566 */
567
568void hv_kvp_onchannelcallback(void *context)
569{
570 struct vmbus_channel *channel = context;
571 u32 recvlen;
572 u64 requestid;
573
574 struct hv_kvp_msg *kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700575
576 struct icmsg_hdr *icmsghdrp;
577 struct icmsg_negotiate *negop = NULL;
578
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700579 if (kvp_transaction.active) {
580 /*
581 * We will defer processing this callback once
582 * the current transaction is complete.
583 */
584 kvp_transaction.kvp_context = context;
585 return;
586 }
Ky Srinivasan245ba562010-12-16 18:54:16 -0700587
K. Y. Srinivasan03db7722012-08-16 18:32:12 -0700588 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
589 &requestid);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700590
591 if (recvlen > 0) {
Ky Srinivasan245ba562010-12-16 18:54:16 -0700592 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
593 sizeof(struct vmbuspipe_hdr)];
594
595 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
K. Y. Srinivasanc836d0a2012-05-12 13:44:58 -0700596 vmbus_prep_negotiate_resp(icmsghdrp, negop,
597 recv_buffer, MAX_SRV_VER, MAX_SRV_VER);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700598 } else {
599 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
600 sizeof(struct vmbuspipe_hdr) +
601 sizeof(struct icmsg_hdr)];
602
Ky Srinivasan245ba562010-12-16 18:54:16 -0700603 /*
604 * Stash away this global state for completing the
605 * transaction; note transactions are serialized.
606 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700607
Ky Srinivasan245ba562010-12-16 18:54:16 -0700608 kvp_transaction.recv_len = recvlen;
609 kvp_transaction.recv_channel = channel;
610 kvp_transaction.recv_req_id = requestid;
611 kvp_transaction.active = true;
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700612 kvp_transaction.kvp_msg = kvp_msg;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700613
614 /*
615 * Get the information from the
616 * user-mode component.
617 * component. This transaction will be
618 * completed when we get the value from
619 * the user-mode component.
620 * Set a timeout to deal with
621 * user-mode not responding.
622 */
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700623 schedule_work(&kvp_sendkey_work);
624 schedule_delayed_work(&kvp_work, 5*HZ);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700625
626 return;
627
628 }
629
Ky Srinivasan245ba562010-12-16 18:54:16 -0700630 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
631 | ICMSGHDRFLAG_RESPONSE;
632
633 vmbus_sendpacket(channel, recv_buffer,
634 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800635 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700636 }
637
638}
639
640int
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700641hv_kvp_init(struct hv_util_service *srv)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700642{
643 int err;
644
645 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
646 if (err)
647 return err;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700648 recv_buffer = srv->recv_buffer;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700649
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700650 /*
651 * When this driver loads, the user level daemon that
652 * processes the host requests may not yet be running.
653 * Defer processing channel callbacks until the daemon
654 * has registered.
655 */
656 kvp_transaction.active = true;
657
Ky Srinivasan245ba562010-12-16 18:54:16 -0700658 return 0;
659}
660
661void hv_kvp_deinit(void)
662{
663 cn_del_callback(&kvp_id);
664 cancel_delayed_work_sync(&kvp_work);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700665 cancel_work_sync(&kvp_sendkey_work);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700666}