Commit | Line | Data |
---|---|---|
245ba56a KS |
1 | /* |
2 | * An implementation of key value pair (KVP) functionality for Linux. | |
3 | * | |
4 | * | |
5 | * Copyright (C) 2010, Novell, Inc. | |
6 | * Author : K. Y. Srinivasan <ksrinivasan@novell.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License version 2 as published | |
10 | * by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
15 | * NON INFRINGEMENT. See the GNU General Public License for more | |
16 | * details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | * | |
22 | */ | |
af7a5b6e | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
245ba56a KS |
24 | |
25 | #include <linux/net.h> | |
26 | #include <linux/nls.h> | |
27 | #include <linux/connector.h> | |
28 | #include <linux/workqueue.h> | |
46a97191 | 29 | #include <linux/hyperv.h> |
245ba56a | 30 | |
245ba56a | 31 | |
6741335b S |
32 | /* |
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) | |
245ba56a KS |
42 | |
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 | ||
52 | static struct { | |
53 | bool active; /* transaction status - active or not */ | |
54 | int recv_len; /* number of bytes received. */ | |
fa3d5b85 | 55 | struct hv_kvp_msg *kvp_msg; /* current message */ |
245ba56a KS |
56 | struct vmbus_channel *recv_channel; /* chn we got the request */ |
57 | u64 recv_req_id; /* request ID. */ | |
fa3d5b85 | 58 | void *kvp_context; /* for the channel callback */ |
245ba56a KS |
59 | } kvp_transaction; |
60 | ||
b47a81dc S |
61 | /* |
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 | */ | |
66 | static bool in_hand_shake = true; | |
67 | ||
68 | /* | |
69 | * This state maintains the version number registered by the daemon. | |
70 | */ | |
71 | static int dm_reg_value; | |
72 | ||
e2bb6537 | 73 | static void kvp_send_key(struct work_struct *dummy); |
245ba56a | 74 | |
76e5f813 | 75 | |
03db7724 | 76 | static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error); |
245ba56a | 77 | static void kvp_work_func(struct work_struct *dummy); |
b47a81dc | 78 | static void kvp_register(int); |
245ba56a KS |
79 | |
80 | static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func); | |
e2bb6537 | 81 | static DECLARE_WORK(kvp_sendkey_work, kvp_send_key); |
245ba56a KS |
82 | |
83 | static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL }; | |
84 | static const char kvp_name[] = "kvp_kernel_module"; | |
245ba56a KS |
85 | static 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 | ||
91 | static void | |
b47a81dc | 92 | kvp_register(int reg_value) |
245ba56a KS |
93 | { |
94 | ||
95 | struct cn_msg *msg; | |
26403354 S |
96 | struct hv_kvp_msg *kvp_msg; |
97 | char *version; | |
245ba56a | 98 | |
26403354 | 99 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC); |
245ba56a KS |
100 | |
101 | if (msg) { | |
26403354 | 102 | kvp_msg = (struct hv_kvp_msg *)msg->data; |
e485ceac | 103 | version = kvp_msg->body.kvp_register.version; |
245ba56a KS |
104 | msg->id.idx = CN_KVP_IDX; |
105 | msg->id.val = CN_KVP_VAL; | |
26403354 | 106 | |
b47a81dc | 107 | kvp_msg->kvp_hdr.operation = reg_value; |
26403354 S |
108 | strcpy(version, HV_DRV_VERSION); |
109 | msg->len = sizeof(struct hv_kvp_msg); | |
245ba56a KS |
110 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
111 | kfree(msg); | |
112 | } | |
113 | } | |
114 | static void | |
115 | kvp_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 | */ | |
03db7724 | 121 | kvp_respond_to_host(NULL, HV_E_FAIL); |
b47a81dc S |
122 | } |
123 | ||
124 | static 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; | |
245ba56a KS |
155 | } |
156 | ||
b47a81dc | 157 | |
245ba56a KS |
158 | /* |
159 | * Callback when data is received from user mode. | |
160 | */ | |
161 | ||
162 | static void | |
163 | kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) | |
164 | { | |
26403354 S |
165 | struct hv_kvp_msg *message; |
166 | struct hv_kvp_msg_enumerate *data; | |
b47a81dc | 167 | int error = 0; |
245ba56a | 168 | |
26403354 | 169 | message = (struct hv_kvp_msg *)msg->data; |
b47a81dc S |
170 | |
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) { | |
fa3d5b85 | 190 | case KVP_OP_REGISTER: |
b47a81dc S |
191 | /* |
192 | * Null string is used to pass back error condition. | |
193 | */ | |
194 | if (data->data.key[0] == 0) | |
195 | error = HV_S_CONT; | |
fa3d5b85 | 196 | break; |
245ba56a | 197 | |
b47a81dc | 198 | case KVP_OP_REGISTER1: |
245ba56a | 199 | /* |
b47a81dc S |
200 | * We use the message header information from |
201 | * the user level daemon to transmit errors. | |
245ba56a | 202 | */ |
b47a81dc S |
203 | error = message->error; |
204 | break; | |
245ba56a | 205 | } |
b47a81dc S |
206 | |
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)) | |
03db7724 | 212 | kvp_respond_to_host(message, error); |
245ba56a KS |
213 | } |
214 | ||
03db7724 S |
215 | |
216 | static 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; | |
a500e0e7 S |
269 | out->kvp_ip_val.addr_family = |
270 | in->body.kvp_ip_val.addr_family; | |
03db7724 S |
271 | } |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
276 | static 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 | ||
e2bb6537 S |
326 | static void |
327 | kvp_send_key(struct work_struct *dummy) | |
245ba56a KS |
328 | { |
329 | struct cn_msg *msg; | |
26403354 | 330 | struct hv_kvp_msg *message; |
fa3d5b85 S |
331 | 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; | |
245ba56a KS |
336 | |
337 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC); | |
fa3d5b85 S |
338 | if (!msg) |
339 | return; | |
245ba56a | 340 | |
fa3d5b85 S |
341 | msg->id.idx = CN_KVP_IDX; |
342 | msg->id.val = CN_KVP_VAL; | |
26403354 | 343 | |
fa3d5b85 S |
344 | 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) { | |
03db7724 S |
361 | 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; | |
fa3d5b85 S |
367 | 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; | |
245ba56a | 429 | } |
fa3d5b85 S |
430 | |
431 | msg->len = sizeof(struct hv_kvp_msg); | |
432 | cn_netlink_send(msg, 0, GFP_ATOMIC); | |
433 | kfree(msg); | |
434 | ||
e2bb6537 | 435 | return; |
245ba56a KS |
436 | } |
437 | ||
438 | /* | |
439 | * Send a response back to the host. | |
440 | */ | |
441 | ||
442 | static void | |
03db7724 | 443 | kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error) |
245ba56a KS |
444 | { |
445 | struct hv_kvp_msg *kvp_msg; | |
fa3d5b85 | 446 | struct hv_kvp_exchg_msg_value *kvp_data; |
245ba56a | 447 | char *key_name; |
03db7724 | 448 | char *value; |
245ba56a | 449 | struct icmsg_hdr *icmsghdrp; |
fa3d5b85 S |
450 | int keylen = 0; |
451 | int valuelen = 0; | |
245ba56a KS |
452 | u32 buf_len; |
453 | struct vmbus_channel *channel; | |
454 | u64 req_id; | |
03db7724 | 455 | int ret; |
245ba56a KS |
456 | |
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 | */ | |
af7a5b6e | 465 | pr_warn("KVP: Transaction not active\n"); |
245ba56a KS |
466 | 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 | ||
76e5f813 S |
477 | kvp_transaction.active = false; |
478 | ||
fa3d5b85 S |
479 | icmsghdrp = (struct icmsg_hdr *) |
480 | &recv_buffer[sizeof(struct vmbuspipe_hdr)]; | |
481 | ||
4e65f6e8 S |
482 | if (channel->onchannel_callback == NULL) |
483 | /* | |
484 | * We have raced with util driver being unloaded; | |
485 | * silently return. | |
486 | */ | |
487 | return; | |
488 | ||
b47a81dc | 489 | icmsghdrp->status = error; |
245ba56a KS |
490 | |
491 | /* | |
adc80ae6 S |
492 | * If the error parameter is set, terminate the host's enumeration |
493 | * on this pool. | |
245ba56a KS |
494 | */ |
495 | if (error) { | |
496 | /* | |
b47a81dc S |
497 | * Something failed or we have timedout; |
498 | * terminate the current host-side iteration. | |
245ba56a | 499 | */ |
245ba56a KS |
500 | goto response_done; |
501 | } | |
502 | ||
fa3d5b85 S |
503 | 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) { | |
03db7724 S |
508 | 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; | |
fa3d5b85 S |
518 | 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; | |
03db7724 | 531 | key_name = msg_to_host->body.kvp_enum_data.data.key; |
fa3d5b85 | 532 | |
245ba56a KS |
533 | /* |
534 | * The windows host expects the key/value pair to be encoded | |
fa3d5b85 S |
535 | * 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). | |
245ba56a | 538 | */ |
0720a06a | 539 | keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN, |
fa3d5b85 S |
540 | (wchar_t *) kvp_data->key, |
541 | (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2); | |
542 | kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */ | |
543 | ||
544 | copy_value: | |
03db7724 | 545 | value = msg_to_host->body.kvp_enum_data.data.value; |
0720a06a | 546 | valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN, |
fa3d5b85 S |
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 */ | |
245ba56a | 550 | |
fa3d5b85 S |
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 */ | |
245ba56a KS |
559 | |
560 | response_done: | |
561 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; | |
562 | ||
563 | vmbus_sendpacket(channel, recv_buffer, buf_len, req_id, | |
415f2287 | 564 | VM_PKT_DATA_INBAND, 0); |
245ba56a | 565 | |
245ba56a KS |
566 | } |
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 | ||
578 | void 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; | |
245ba56a KS |
585 | |
586 | struct icmsg_hdr *icmsghdrp; | |
587 | struct icmsg_negotiate *negop = NULL; | |
588 | ||
fa3d5b85 S |
589 | 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 | } | |
245ba56a | 597 | |
03db7724 S |
598 | vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen, |
599 | &requestid); | |
245ba56a KS |
600 | |
601 | if (recvlen > 0) { | |
245ba56a KS |
602 | icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ |
603 | sizeof(struct vmbuspipe_hdr)]; | |
604 | ||
605 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
6741335b S |
606 | /* |
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 | ||
c836d0ab | 615 | vmbus_prep_negotiate_resp(icmsghdrp, negop, |
6741335b S |
616 | recv_buffer, UTIL_FW_MAJOR_MINOR, |
617 | WIN7_SRV_MAJOR_MINOR); | |
618 | ||
245ba56a KS |
619 | } else { |
620 | kvp_msg = (struct hv_kvp_msg *)&recv_buffer[ | |
621 | sizeof(struct vmbuspipe_hdr) + | |
622 | sizeof(struct icmsg_hdr)]; | |
623 | ||
245ba56a KS |
624 | /* |
625 | * Stash away this global state for completing the | |
626 | * transaction; note transactions are serialized. | |
627 | */ | |
fa3d5b85 | 628 | |
245ba56a KS |
629 | kvp_transaction.recv_len = recvlen; |
630 | kvp_transaction.recv_channel = channel; | |
631 | kvp_transaction.recv_req_id = requestid; | |
632 | kvp_transaction.active = true; | |
fa3d5b85 | 633 | kvp_transaction.kvp_msg = kvp_msg; |
245ba56a KS |
634 | |
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 | */ | |
e2bb6537 S |
644 | schedule_work(&kvp_sendkey_work); |
645 | schedule_delayed_work(&kvp_work, 5*HZ); | |
245ba56a KS |
646 | |
647 | return; | |
648 | ||
649 | } | |
6741335b | 650 | done: |
245ba56a | 651 | |
245ba56a KS |
652 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
653 | | ICMSGHDRFLAG_RESPONSE; | |
654 | ||
655 | vmbus_sendpacket(channel, recv_buffer, | |
656 | recvlen, requestid, | |
415f2287 | 657 | VM_PKT_DATA_INBAND, 0); |
245ba56a KS |
658 | } |
659 | ||
660 | } | |
661 | ||
662 | int | |
a29b643c | 663 | hv_kvp_init(struct hv_util_service *srv) |
245ba56a KS |
664 | { |
665 | int err; | |
666 | ||
667 | err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback); | |
668 | if (err) | |
669 | return err; | |
a29b643c | 670 | recv_buffer = srv->recv_buffer; |
245ba56a | 671 | |
fa3d5b85 S |
672 | /* |
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 | ||
245ba56a KS |
680 | return 0; |
681 | } | |
682 | ||
683 | void hv_kvp_deinit(void) | |
684 | { | |
685 | cn_del_callback(&kvp_id); | |
686 | cancel_delayed_work_sync(&kvp_work); | |
e2bb6537 | 687 | cancel_work_sync(&kvp_sendkey_work); |
245ba56a | 688 | } |