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, 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
)
134 spin_lock_irqsave(&channel
->inbound_lock
, flags
);
135 hv_kvp_onchannelcallback(channel
);
136 spin_unlock_irqrestore(&channel
->inbound_lock
, flags
);
139 static int kvp_handle_handshake(struct hv_kvp_msg
*msg
)
143 switch (msg
->kvp_hdr
.operation
) {
144 case KVP_OP_REGISTER
:
145 dm_reg_value
= KVP_OP_REGISTER
;
146 pr_info("KVP: IP injection functionality not available\n");
147 pr_info("KVP: Upgrade the KVP daemon\n");
149 case KVP_OP_REGISTER1
:
150 dm_reg_value
= KVP_OP_REGISTER1
;
153 pr_info("KVP: incompatible daemon\n");
154 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
155 KVP_OP_REGISTER1
, msg
->kvp_hdr
.operation
);
161 * We have a compatible daemon; complete the handshake.
163 pr_info("KVP: user-mode registering done.\n");
164 kvp_register(dm_reg_value
);
165 kvp_transaction
.active
= false;
166 if (kvp_transaction
.kvp_context
)
167 poll_channel(kvp_transaction
.kvp_context
);
174 * Callback when data is received from user mode.
178 kvp_cn_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
180 struct hv_kvp_msg
*message
;
181 struct hv_kvp_msg_enumerate
*data
;
184 message
= (struct hv_kvp_msg
*)msg
->data
;
187 * If we are negotiating the version information
188 * with the daemon; handle that first.
192 if (kvp_handle_handshake(message
))
193 in_hand_shake
= false;
198 * Based on the version of the daemon, we propagate errors from the
199 * daemon differently.
202 data
= &message
->body
.kvp_enum_data
;
204 switch (dm_reg_value
) {
205 case KVP_OP_REGISTER
:
207 * Null string is used to pass back error condition.
209 if (data
->data
.key
[0] == 0)
213 case KVP_OP_REGISTER1
:
215 * We use the message header information from
216 * the user level daemon to transmit errors.
218 error
= message
->error
;
223 * Complete the transaction by forwarding the key value
224 * to the host. But first, cancel the timeout.
226 if (cancel_delayed_work_sync(&kvp_work
))
227 kvp_respond_to_host(message
, error
);
231 static int process_ob_ipinfo(void *in_msg
, void *out_msg
, int op
)
233 struct hv_kvp_msg
*in
= in_msg
;
234 struct hv_kvp_ip_msg
*out
= out_msg
;
238 case KVP_OP_GET_IP_INFO
:
240 * Transform all parameters into utf16 encoding.
242 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.ip_addr
,
243 strlen((char *)in
->body
.kvp_ip_val
.ip_addr
),
245 (wchar_t *)out
->kvp_ip_val
.ip_addr
,
250 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.sub_net
,
251 strlen((char *)in
->body
.kvp_ip_val
.sub_net
),
253 (wchar_t *)out
->kvp_ip_val
.sub_net
,
258 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.gate_way
,
259 strlen((char *)in
->body
.kvp_ip_val
.gate_way
),
261 (wchar_t *)out
->kvp_ip_val
.gate_way
,
266 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.dns_addr
,
267 strlen((char *)in
->body
.kvp_ip_val
.dns_addr
),
269 (wchar_t *)out
->kvp_ip_val
.dns_addr
,
274 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.adapter_id
,
275 strlen((char *)in
->body
.kvp_ip_val
.adapter_id
),
277 (wchar_t *)out
->kvp_ip_val
.adapter_id
,
282 out
->kvp_ip_val
.dhcp_enabled
=
283 in
->body
.kvp_ip_val
.dhcp_enabled
;
284 out
->kvp_ip_val
.addr_family
=
285 in
->body
.kvp_ip_val
.addr_family
;
291 static void process_ib_ipinfo(void *in_msg
, void *out_msg
, int op
)
293 struct hv_kvp_ip_msg
*in
= in_msg
;
294 struct hv_kvp_msg
*out
= out_msg
;
297 case KVP_OP_SET_IP_INFO
:
299 * Transform all parameters into utf8 encoding.
301 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.ip_addr
,
304 (__u8
*)out
->body
.kvp_ip_val
.ip_addr
,
307 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.sub_net
,
310 (__u8
*)out
->body
.kvp_ip_val
.sub_net
,
313 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.gate_way
,
316 (__u8
*)out
->body
.kvp_ip_val
.gate_way
,
319 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.dns_addr
,
322 (__u8
*)out
->body
.kvp_ip_val
.dns_addr
,
325 out
->body
.kvp_ip_val
.dhcp_enabled
= in
->kvp_ip_val
.dhcp_enabled
;
328 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.adapter_id
,
331 (__u8
*)out
->body
.kvp_ip_val
.adapter_id
,
332 MAX_ADAPTER_ID_SIZE
);
334 out
->body
.kvp_ip_val
.addr_family
= in
->kvp_ip_val
.addr_family
;
342 kvp_send_key(struct work_struct
*dummy
)
345 struct hv_kvp_msg
*message
;
346 struct hv_kvp_msg
*in_msg
;
347 __u8 operation
= kvp_transaction
.kvp_msg
->kvp_hdr
.operation
;
348 __u8 pool
= kvp_transaction
.kvp_msg
->kvp_hdr
.pool
;
352 msg
= kzalloc(sizeof(*msg
) + sizeof(struct hv_kvp_msg
) , GFP_ATOMIC
);
356 msg
->id
.idx
= CN_KVP_IDX
;
357 msg
->id
.val
= CN_KVP_VAL
;
359 message
= (struct hv_kvp_msg
*)msg
->data
;
360 message
->kvp_hdr
.operation
= operation
;
361 message
->kvp_hdr
.pool
= pool
;
362 in_msg
= kvp_transaction
.kvp_msg
;
365 * The key/value strings sent from the host are encoded in
366 * in utf16; convert it to utf8 strings.
367 * The host assures us that the utf16 strings will not exceed
368 * the max lengths specified. We will however, reserve room
369 * for the string terminating character - in the utf16s_utf8s()
370 * function we limit the size of the buffer where the converted
371 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
372 * that the strings can be properly terminated!
375 switch (message
->kvp_hdr
.operation
) {
376 case KVP_OP_SET_IP_INFO
:
377 process_ib_ipinfo(in_msg
, message
, KVP_OP_SET_IP_INFO
);
379 case KVP_OP_GET_IP_INFO
:
380 process_ib_ipinfo(in_msg
, message
, KVP_OP_GET_IP_INFO
);
383 switch (in_msg
->body
.kvp_set
.data
.value_type
) {
386 * The value is a string - utf16 encoding.
388 message
->body
.kvp_set
.data
.value_size
=
390 (wchar_t *)in_msg
->body
.kvp_set
.data
.value
,
391 in_msg
->body
.kvp_set
.data
.value_size
,
393 message
->body
.kvp_set
.data
.value
,
394 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
- 1) + 1;
399 * The value is a 32 bit scalar.
400 * We save this as a utf8 string.
402 val32
= in_msg
->body
.kvp_set
.data
.value_u32
;
403 message
->body
.kvp_set
.data
.value_size
=
404 sprintf(message
->body
.kvp_set
.data
.value
,
410 * The value is a 64 bit scalar.
411 * We save this as a utf8 string.
413 val64
= in_msg
->body
.kvp_set
.data
.value_u64
;
414 message
->body
.kvp_set
.data
.value_size
=
415 sprintf(message
->body
.kvp_set
.data
.value
,
421 message
->body
.kvp_set
.data
.key_size
=
423 (wchar_t *)in_msg
->body
.kvp_set
.data
.key
,
424 in_msg
->body
.kvp_set
.data
.key_size
,
426 message
->body
.kvp_set
.data
.key
,
427 HV_KVP_EXCHANGE_MAX_KEY_SIZE
- 1) + 1;
431 message
->body
.kvp_delete
.key_size
=
433 (wchar_t *)in_msg
->body
.kvp_delete
.key
,
434 in_msg
->body
.kvp_delete
.key_size
,
436 message
->body
.kvp_delete
.key
,
437 HV_KVP_EXCHANGE_MAX_KEY_SIZE
- 1) + 1;
440 case KVP_OP_ENUMERATE
:
441 message
->body
.kvp_enum_data
.index
=
442 in_msg
->body
.kvp_enum_data
.index
;
446 msg
->len
= sizeof(struct hv_kvp_msg
);
447 cn_netlink_send(msg
, 0, GFP_ATOMIC
);
454 * Send a response back to the host.
458 kvp_respond_to_host(struct hv_kvp_msg
*msg_to_host
, int error
)
460 struct hv_kvp_msg
*kvp_msg
;
461 struct hv_kvp_exchg_msg_value
*kvp_data
;
464 struct icmsg_hdr
*icmsghdrp
;
468 struct vmbus_channel
*channel
;
473 * If a transaction is not active; log and return.
476 if (!kvp_transaction
.active
) {
478 * This is a spurious call!
480 pr_warn("KVP: Transaction not active\n");
484 * Copy the global state for completing the transaction. Note that
485 * only one transaction can be active at a time.
488 buf_len
= kvp_transaction
.recv_len
;
489 channel
= kvp_transaction
.recv_channel
;
490 req_id
= kvp_transaction
.recv_req_id
;
492 kvp_transaction
.active
= false;
494 icmsghdrp
= (struct icmsg_hdr
*)
495 &recv_buffer
[sizeof(struct vmbuspipe_hdr
)];
497 if (channel
->onchannel_callback
== NULL
)
499 * We have raced with util driver being unloaded;
504 icmsghdrp
->status
= error
;
507 * If the error parameter is set, terminate the host's enumeration
512 * Something failed or we have timedout;
513 * terminate the current host-side iteration.
518 kvp_msg
= (struct hv_kvp_msg
*)
519 &recv_buffer
[sizeof(struct vmbuspipe_hdr
) +
520 sizeof(struct icmsg_hdr
)];
522 switch (kvp_transaction
.kvp_msg
->kvp_hdr
.operation
) {
523 case KVP_OP_GET_IP_INFO
:
524 ret
= process_ob_ipinfo(msg_to_host
,
525 (struct hv_kvp_ip_msg
*)kvp_msg
,
528 icmsghdrp
->status
= HV_E_FAIL
;
531 case KVP_OP_SET_IP_INFO
:
534 kvp_data
= &kvp_msg
->body
.kvp_get
.data
;
545 kvp_data
= &kvp_msg
->body
.kvp_enum_data
.data
;
546 key_name
= msg_to_host
->body
.kvp_enum_data
.data
.key
;
549 * The windows host expects the key/value pair to be encoded
550 * in utf16. Ensure that the key/value size reported to the host
551 * will be less than or equal to the MAX size (including the
552 * terminating character).
554 keylen
= utf8s_to_utf16s(key_name
, strlen(key_name
), UTF16_HOST_ENDIAN
,
555 (wchar_t *) kvp_data
->key
,
556 (HV_KVP_EXCHANGE_MAX_KEY_SIZE
/ 2) - 2);
557 kvp_data
->key_size
= 2*(keylen
+ 1); /* utf16 encoding */
560 value
= msg_to_host
->body
.kvp_enum_data
.data
.value
;
561 valuelen
= utf8s_to_utf16s(value
, strlen(value
), UTF16_HOST_ENDIAN
,
562 (wchar_t *) kvp_data
->value
,
563 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE
/ 2) - 2);
564 kvp_data
->value_size
= 2*(valuelen
+ 1); /* utf16 encoding */
567 * If the utf8s to utf16s conversion failed; notify host
570 if ((keylen
< 0) || (valuelen
< 0))
571 icmsghdrp
->status
= HV_E_FAIL
;
573 kvp_data
->value_type
= REG_SZ
; /* all our values are strings */
576 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
578 vmbus_sendpacket(channel
, recv_buffer
, buf_len
, req_id
,
579 VM_PKT_DATA_INBAND
, 0);
580 poll_channel(channel
);
585 * This callback is invoked when we get a KVP message from the host.
586 * The host ensures that only one KVP transaction can be active at a time.
587 * KVP implementation in Linux needs to forward the key to a user-mde
588 * component to retrive the corresponding value. Consequently, we cannot
589 * respond to the host in the conext of this callback. Since the host
590 * guarantees that at most only one transaction can be active at a time,
591 * we stash away the transaction state in a set of global variables.
594 void hv_kvp_onchannelcallback(void *context
)
596 struct vmbus_channel
*channel
= context
;
600 struct hv_kvp_msg
*kvp_msg
;
602 struct icmsg_hdr
*icmsghdrp
;
603 struct icmsg_negotiate
*negop
= NULL
;
607 if (kvp_transaction
.active
) {
609 * We will defer processing this callback once
610 * the current transaction is complete.
612 kvp_transaction
.kvp_context
= context
;
616 vmbus_recvpacket(channel
, recv_buffer
, PAGE_SIZE
* 4, &recvlen
,
620 icmsghdrp
= (struct icmsg_hdr
*)&recv_buffer
[
621 sizeof(struct vmbuspipe_hdr
)];
623 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
625 * Based on the host, select appropriate
626 * framework and service versions we will
629 switch (vmbus_proto_version
) {
630 case (VERSION_WS2008
):
631 util_fw_version
= UTIL_WS2K8_FW_VERSION
;
632 kvp_srv_version
= WS2008_SRV_VERSION
;
635 util_fw_version
= UTIL_FW_VERSION
;
636 kvp_srv_version
= WIN7_SRV_VERSION
;
639 util_fw_version
= UTIL_FW_VERSION
;
640 kvp_srv_version
= WIN8_SRV_VERSION
;
642 vmbus_prep_negotiate_resp(icmsghdrp
, negop
,
643 recv_buffer
, util_fw_version
,
647 kvp_msg
= (struct hv_kvp_msg
*)&recv_buffer
[
648 sizeof(struct vmbuspipe_hdr
) +
649 sizeof(struct icmsg_hdr
)];
652 * Stash away this global state for completing the
653 * transaction; note transactions are serialized.
656 kvp_transaction
.recv_len
= recvlen
;
657 kvp_transaction
.recv_channel
= channel
;
658 kvp_transaction
.recv_req_id
= requestid
;
659 kvp_transaction
.active
= true;
660 kvp_transaction
.kvp_msg
= kvp_msg
;
663 * Get the information from the
664 * user-mode component.
665 * component. This transaction will be
666 * completed when we get the value from
667 * the user-mode component.
668 * Set a timeout to deal with
669 * user-mode not responding.
671 schedule_work(&kvp_sendkey_work
);
672 schedule_delayed_work(&kvp_work
, 5*HZ
);
678 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
679 | ICMSGHDRFLAG_RESPONSE
;
681 vmbus_sendpacket(channel
, recv_buffer
,
683 VM_PKT_DATA_INBAND
, 0);
689 hv_kvp_init(struct hv_util_service
*srv
)
693 err
= cn_add_callback(&kvp_id
, kvp_name
, kvp_cn_callback
);
696 recv_buffer
= srv
->recv_buffer
;
699 * When this driver loads, the user level daemon that
700 * processes the host requests may not yet be running.
701 * Defer processing channel callbacks until the daemon
704 kvp_transaction
.active
= true;
709 void hv_kvp_deinit(void)
711 cn_del_callback(&kvp_id
);
712 cancel_delayed_work_sync(&kvp_work
);
713 cancel_work_sync(&kvp_sendkey_work
);