2 * An implementation of key value pair (KVP) functionality for Linux.
5 * Copyright (C) 2010, Novell, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
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.
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
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.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/net.h>
26 #include <linux/nls.h>
27 #include <linux/connector.h>
28 #include <linux/workqueue.h>
29 #include <linux/hyperv.h>
33 * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
35 #define WS2008_SRV_MAJOR 1
36 #define WS2008_SRV_MINOR 0
37 #define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
39 #define WIN7_SRV_MAJOR 3
40 #define WIN7_SRV_MINOR 0
41 #define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
43 #define WIN8_SRV_MAJOR 4
44 #define WIN8_SRV_MINOR 0
45 #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
48 * Global state maintained for transaction that is being processed.
49 * Note that only one transaction can be active at any point in time.
51 * This state is set when we receive a request from the host; we
52 * cleanup this state when the transaction is completed - when we respond
53 * to the host with the key value.
57 bool active
; /* transaction status - active or not */
58 int recv_len
; /* number of bytes received. */
59 struct hv_kvp_msg
*kvp_msg
; /* current message */
60 struct vmbus_channel
*recv_channel
; /* chn we got the request */
61 u64 recv_req_id
; /* request ID. */
62 void *kvp_context
; /* for the channel callback */
66 * Before we can accept KVP messages from the host, we need
67 * to handshake with the user level daemon. This state tracks
68 * if we are in the handshake phase.
70 static bool in_hand_shake
= true;
73 * This state maintains the version number registered by the daemon.
75 static int dm_reg_value
;
77 static void kvp_send_key(struct work_struct
*dummy
);
80 static void kvp_respond_to_host(struct hv_kvp_msg
*msg
, int error
);
81 static void kvp_work_func(struct work_struct
*dummy
);
82 static void kvp_register(int);
84 static DECLARE_DELAYED_WORK(kvp_work
, kvp_work_func
);
85 static DECLARE_WORK(kvp_sendkey_work
, kvp_send_key
);
87 static struct cb_id kvp_id
= { CN_KVP_IDX
, CN_KVP_VAL
};
88 static const char kvp_name
[] = "kvp_kernel_module";
89 static u8
*recv_buffer
;
91 * Register the kernel component with the user-level daemon.
92 * As part of this registration, pass the LIC version number.
93 * This number has no meaning, it satisfies the registration protocol.
95 #define HV_DRV_VERSION "3.1"
98 kvp_register(int reg_value
)
102 struct hv_kvp_msg
*kvp_msg
;
105 msg
= kzalloc(sizeof(*msg
) + sizeof(struct hv_kvp_msg
), GFP_ATOMIC
);
108 kvp_msg
= (struct hv_kvp_msg
*)msg
->data
;
109 version
= kvp_msg
->body
.kvp_register
.version
;
110 msg
->id
.idx
= CN_KVP_IDX
;
111 msg
->id
.val
= CN_KVP_VAL
;
113 kvp_msg
->kvp_hdr
.operation
= reg_value
;
114 strcpy(version
, HV_DRV_VERSION
);
115 msg
->len
= sizeof(struct hv_kvp_msg
);
116 cn_netlink_send(msg
, 0, 0, GFP_ATOMIC
);
121 kvp_work_func(struct work_struct
*dummy
)
124 * If the timer fires, the user-mode component has not responded;
125 * process the pending transaction.
127 kvp_respond_to_host(NULL
, HV_E_FAIL
);
130 static void poll_channel(struct vmbus_channel
*channel
)
132 if (channel
->target_cpu
!= smp_processor_id())
133 smp_call_function_single(channel
->target_cpu
,
134 hv_kvp_onchannelcallback
,
137 hv_kvp_onchannelcallback(channel
);
141 static int kvp_handle_handshake(struct hv_kvp_msg
*msg
)
145 switch (msg
->kvp_hdr
.operation
) {
146 case KVP_OP_REGISTER
:
147 dm_reg_value
= KVP_OP_REGISTER
;
148 pr_info("KVP: IP injection functionality not available\n");
149 pr_info("KVP: Upgrade the KVP daemon\n");
151 case KVP_OP_REGISTER1
:
152 dm_reg_value
= KVP_OP_REGISTER1
;
155 pr_info("KVP: incompatible daemon\n");
156 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
157 KVP_OP_REGISTER1
, msg
->kvp_hdr
.operation
);
163 * We have a compatible daemon; complete the handshake.
165 pr_info("KVP: user-mode registering done.\n");
166 kvp_register(dm_reg_value
);
167 kvp_transaction
.active
= false;
168 if (kvp_transaction
.kvp_context
)
169 poll_channel(kvp_transaction
.kvp_context
);
176 * Callback when data is received from user mode.
180 kvp_cn_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
182 struct hv_kvp_msg
*message
;
183 struct hv_kvp_msg_enumerate
*data
;
186 message
= (struct hv_kvp_msg
*)msg
->data
;
189 * If we are negotiating the version information
190 * with the daemon; handle that first.
194 if (kvp_handle_handshake(message
))
195 in_hand_shake
= false;
200 * Based on the version of the daemon, we propagate errors from the
201 * daemon differently.
204 data
= &message
->body
.kvp_enum_data
;
206 switch (dm_reg_value
) {
207 case KVP_OP_REGISTER
:
209 * Null string is used to pass back error condition.
211 if (data
->data
.key
[0] == 0)
215 case KVP_OP_REGISTER1
:
217 * We use the message header information from
218 * the user level daemon to transmit errors.
220 error
= message
->error
;
225 * Complete the transaction by forwarding the key value
226 * to the host. But first, cancel the timeout.
228 if (cancel_delayed_work_sync(&kvp_work
))
229 kvp_respond_to_host(message
, error
);
233 static int process_ob_ipinfo(void *in_msg
, void *out_msg
, int op
)
235 struct hv_kvp_msg
*in
= in_msg
;
236 struct hv_kvp_ip_msg
*out
= out_msg
;
240 case KVP_OP_GET_IP_INFO
:
242 * Transform all parameters into utf16 encoding.
244 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.ip_addr
,
245 strlen((char *)in
->body
.kvp_ip_val
.ip_addr
),
247 (wchar_t *)out
->kvp_ip_val
.ip_addr
,
252 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.sub_net
,
253 strlen((char *)in
->body
.kvp_ip_val
.sub_net
),
255 (wchar_t *)out
->kvp_ip_val
.sub_net
,
260 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.gate_way
,
261 strlen((char *)in
->body
.kvp_ip_val
.gate_way
),
263 (wchar_t *)out
->kvp_ip_val
.gate_way
,
268 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.dns_addr
,
269 strlen((char *)in
->body
.kvp_ip_val
.dns_addr
),
271 (wchar_t *)out
->kvp_ip_val
.dns_addr
,
276 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.adapter_id
,
277 strlen((char *)in
->body
.kvp_ip_val
.adapter_id
),
279 (wchar_t *)out
->kvp_ip_val
.adapter_id
,
284 out
->kvp_ip_val
.dhcp_enabled
=
285 in
->body
.kvp_ip_val
.dhcp_enabled
;
286 out
->kvp_ip_val
.addr_family
=
287 in
->body
.kvp_ip_val
.addr_family
;
293 static void process_ib_ipinfo(void *in_msg
, void *out_msg
, int op
)
295 struct hv_kvp_ip_msg
*in
= in_msg
;
296 struct hv_kvp_msg
*out
= out_msg
;
299 case KVP_OP_SET_IP_INFO
:
301 * Transform all parameters into utf8 encoding.
303 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.ip_addr
,
306 (__u8
*)out
->body
.kvp_ip_val
.ip_addr
,
309 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.sub_net
,
312 (__u8
*)out
->body
.kvp_ip_val
.sub_net
,
315 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.gate_way
,
318 (__u8
*)out
->body
.kvp_ip_val
.gate_way
,
321 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.dns_addr
,
324 (__u8
*)out
->body
.kvp_ip_val
.dns_addr
,
327 out
->body
.kvp_ip_val
.dhcp_enabled
= in
->kvp_ip_val
.dhcp_enabled
;
330 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.adapter_id
,
333 (__u8
*)out
->body
.kvp_ip_val
.adapter_id
,
334 MAX_ADAPTER_ID_SIZE
);
336 out
->body
.kvp_ip_val
.addr_family
= in
->kvp_ip_val
.addr_family
;
344 kvp_send_key(struct work_struct
*dummy
)
347 struct hv_kvp_msg
*message
;
348 struct hv_kvp_msg
*in_msg
;
349 __u8 operation
= kvp_transaction
.kvp_msg
->kvp_hdr
.operation
;
350 __u8 pool
= kvp_transaction
.kvp_msg
->kvp_hdr
.pool
;
355 msg
= kzalloc(sizeof(*msg
) + sizeof(struct hv_kvp_msg
) , GFP_ATOMIC
);
359 msg
->id
.idx
= CN_KVP_IDX
;
360 msg
->id
.val
= CN_KVP_VAL
;
362 message
= (struct hv_kvp_msg
*)msg
->data
;
363 message
->kvp_hdr
.operation
= operation
;
364 message
->kvp_hdr
.pool
= pool
;
365 in_msg
= kvp_transaction
.kvp_msg
;
368 * The key/value strings sent from the host are encoded in
369 * in utf16; convert it to utf8 strings.
370 * The host assures us that the utf16 strings will not exceed
371 * the max lengths specified. We will however, reserve room
372 * for the string terminating character - in the utf16s_utf8s()
373 * function we limit the size of the buffer where the converted
374 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
375 * that the strings can be properly terminated!
378 switch (message
->kvp_hdr
.operation
) {
379 case KVP_OP_SET_IP_INFO
:
380 process_ib_ipinfo(in_msg
, message
, KVP_OP_SET_IP_INFO
);
382 case KVP_OP_GET_IP_INFO
:
383 process_ib_ipinfo(in_msg
, message
, KVP_OP_GET_IP_INFO
);
386 switch (in_msg
->body
.kvp_set
.data
.value_type
) {
389 * The value is a string - utf16 encoding.
391 message
->body
.kvp_set
.data
.value_size
=
393 (wchar_t *)in_msg
->body
.kvp_set
.data
.value
,
394 in_msg
->body
.kvp_set
.data
.value_size
,
396 message
->body
.kvp_set
.data
.value
,
397 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
- 1) + 1;
402 * The value is a 32 bit scalar.
403 * We save this as a utf8 string.
405 val32
= in_msg
->body
.kvp_set
.data
.value_u32
;
406 message
->body
.kvp_set
.data
.value_size
=
407 sprintf(message
->body
.kvp_set
.data
.value
,
413 * The value is a 64 bit scalar.
414 * We save this as a utf8 string.
416 val64
= in_msg
->body
.kvp_set
.data
.value_u64
;
417 message
->body
.kvp_set
.data
.value_size
=
418 sprintf(message
->body
.kvp_set
.data
.value
,
424 message
->body
.kvp_set
.data
.key_size
=
426 (wchar_t *)in_msg
->body
.kvp_set
.data
.key
,
427 in_msg
->body
.kvp_set
.data
.key_size
,
429 message
->body
.kvp_set
.data
.key
,
430 HV_KVP_EXCHANGE_MAX_KEY_SIZE
- 1) + 1;
434 message
->body
.kvp_delete
.key_size
=
436 (wchar_t *)in_msg
->body
.kvp_delete
.key
,
437 in_msg
->body
.kvp_delete
.key_size
,
439 message
->body
.kvp_delete
.key
,
440 HV_KVP_EXCHANGE_MAX_KEY_SIZE
- 1) + 1;
443 case KVP_OP_ENUMERATE
:
444 message
->body
.kvp_enum_data
.index
=
445 in_msg
->body
.kvp_enum_data
.index
;
449 msg
->len
= sizeof(struct hv_kvp_msg
);
450 rc
= cn_netlink_send(msg
, 0, 0, GFP_ATOMIC
);
452 pr_debug("KVP: failed to communicate to the daemon: %d\n", rc
);
453 if (cancel_delayed_work_sync(&kvp_work
))
454 kvp_respond_to_host(message
, HV_E_FAIL
);
463 * Send a response back to the host.
467 kvp_respond_to_host(struct hv_kvp_msg
*msg_to_host
, int error
)
469 struct hv_kvp_msg
*kvp_msg
;
470 struct hv_kvp_exchg_msg_value
*kvp_data
;
473 struct icmsg_hdr
*icmsghdrp
;
477 struct vmbus_channel
*channel
;
482 * If a transaction is not active; log and return.
485 if (!kvp_transaction
.active
) {
487 * This is a spurious call!
489 pr_warn("KVP: Transaction not active\n");
493 * Copy the global state for completing the transaction. Note that
494 * only one transaction can be active at a time.
497 buf_len
= kvp_transaction
.recv_len
;
498 channel
= kvp_transaction
.recv_channel
;
499 req_id
= kvp_transaction
.recv_req_id
;
501 kvp_transaction
.active
= false;
503 icmsghdrp
= (struct icmsg_hdr
*)
504 &recv_buffer
[sizeof(struct vmbuspipe_hdr
)];
506 if (channel
->onchannel_callback
== NULL
)
508 * We have raced with util driver being unloaded;
513 icmsghdrp
->status
= error
;
516 * If the error parameter is set, terminate the host's enumeration
521 * Something failed or we have timedout;
522 * terminate the current host-side iteration.
527 kvp_msg
= (struct hv_kvp_msg
*)
528 &recv_buffer
[sizeof(struct vmbuspipe_hdr
) +
529 sizeof(struct icmsg_hdr
)];
531 switch (kvp_transaction
.kvp_msg
->kvp_hdr
.operation
) {
532 case KVP_OP_GET_IP_INFO
:
533 ret
= process_ob_ipinfo(msg_to_host
,
534 (struct hv_kvp_ip_msg
*)kvp_msg
,
537 icmsghdrp
->status
= HV_E_FAIL
;
540 case KVP_OP_SET_IP_INFO
:
543 kvp_data
= &kvp_msg
->body
.kvp_get
.data
;
554 kvp_data
= &kvp_msg
->body
.kvp_enum_data
.data
;
555 key_name
= msg_to_host
->body
.kvp_enum_data
.data
.key
;
558 * The windows host expects the key/value pair to be encoded
559 * in utf16. Ensure that the key/value size reported to the host
560 * will be less than or equal to the MAX size (including the
561 * terminating character).
563 keylen
= utf8s_to_utf16s(key_name
, strlen(key_name
), UTF16_HOST_ENDIAN
,
564 (wchar_t *) kvp_data
->key
,
565 (HV_KVP_EXCHANGE_MAX_KEY_SIZE
/ 2) - 2);
566 kvp_data
->key_size
= 2*(keylen
+ 1); /* utf16 encoding */
569 value
= msg_to_host
->body
.kvp_enum_data
.data
.value
;
570 valuelen
= utf8s_to_utf16s(value
, strlen(value
), UTF16_HOST_ENDIAN
,
571 (wchar_t *) kvp_data
->value
,
572 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE
/ 2) - 2);
573 kvp_data
->value_size
= 2*(valuelen
+ 1); /* utf16 encoding */
576 * If the utf8s to utf16s conversion failed; notify host
579 if ((keylen
< 0) || (valuelen
< 0))
580 icmsghdrp
->status
= HV_E_FAIL
;
582 kvp_data
->value_type
= REG_SZ
; /* all our values are strings */
585 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
587 vmbus_sendpacket(channel
, recv_buffer
, buf_len
, req_id
,
588 VM_PKT_DATA_INBAND
, 0);
589 poll_channel(channel
);
593 * This callback is invoked when we get a KVP message from the host.
594 * The host ensures that only one KVP transaction can be active at a time.
595 * KVP implementation in Linux needs to forward the key to a user-mde
596 * component to retrive the corresponding value. Consequently, we cannot
597 * respond to the host in the conext of this callback. Since the host
598 * guarantees that at most only one transaction can be active at a time,
599 * we stash away the transaction state in a set of global variables.
602 void hv_kvp_onchannelcallback(void *context
)
604 struct vmbus_channel
*channel
= context
;
608 struct hv_kvp_msg
*kvp_msg
;
610 struct icmsg_hdr
*icmsghdrp
;
611 struct icmsg_negotiate
*negop
= NULL
;
615 if (kvp_transaction
.active
) {
617 * We will defer processing this callback once
618 * the current transaction is complete.
620 kvp_transaction
.kvp_context
= context
;
624 vmbus_recvpacket(channel
, recv_buffer
, PAGE_SIZE
* 4, &recvlen
,
628 icmsghdrp
= (struct icmsg_hdr
*)&recv_buffer
[
629 sizeof(struct vmbuspipe_hdr
)];
631 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
633 * Based on the host, select appropriate
634 * framework and service versions we will
637 switch (vmbus_proto_version
) {
638 case (VERSION_WS2008
):
639 util_fw_version
= UTIL_WS2K8_FW_VERSION
;
640 kvp_srv_version
= WS2008_SRV_VERSION
;
643 util_fw_version
= UTIL_FW_VERSION
;
644 kvp_srv_version
= WIN7_SRV_VERSION
;
647 util_fw_version
= UTIL_FW_VERSION
;
648 kvp_srv_version
= WIN8_SRV_VERSION
;
650 vmbus_prep_negotiate_resp(icmsghdrp
, negop
,
651 recv_buffer
, util_fw_version
,
655 kvp_msg
= (struct hv_kvp_msg
*)&recv_buffer
[
656 sizeof(struct vmbuspipe_hdr
) +
657 sizeof(struct icmsg_hdr
)];
660 * Stash away this global state for completing the
661 * transaction; note transactions are serialized.
664 kvp_transaction
.recv_len
= recvlen
;
665 kvp_transaction
.recv_channel
= channel
;
666 kvp_transaction
.recv_req_id
= requestid
;
667 kvp_transaction
.active
= true;
668 kvp_transaction
.kvp_msg
= kvp_msg
;
671 * Get the information from the
672 * user-mode component.
673 * component. This transaction will be
674 * completed when we get the value from
675 * the user-mode component.
676 * Set a timeout to deal with
677 * user-mode not responding.
679 schedule_work(&kvp_sendkey_work
);
680 schedule_delayed_work(&kvp_work
, 5*HZ
);
686 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
687 | ICMSGHDRFLAG_RESPONSE
;
689 vmbus_sendpacket(channel
, recv_buffer
,
691 VM_PKT_DATA_INBAND
, 0);
697 hv_kvp_init(struct hv_util_service
*srv
)
701 err
= cn_add_callback(&kvp_id
, kvp_name
, kvp_cn_callback
);
704 recv_buffer
= srv
->recv_buffer
;
707 * When this driver loads, the user level daemon that
708 * processes the host requests may not yet be running.
709 * Defer processing channel callbacks until the daemon
712 kvp_transaction
.active
= true;
717 void hv_kvp_deinit(void)
719 cn_del_callback(&kvp_id
);
720 cancel_delayed_work_sync(&kvp_work
);
721 cancel_work_sync(&kvp_sendkey_work
);