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>
34 * Global state maintained for transaction that is being processed.
35 * Note that only one transaction can be active at any point in time.
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.
43 bool active
; /* transaction status - active or not */
44 int recv_len
; /* number of bytes received. */
45 struct hv_kvp_msg
*kvp_msg
; /* current message */
46 struct vmbus_channel
*recv_channel
; /* chn we got the request */
47 u64 recv_req_id
; /* request ID. */
48 void *kvp_context
; /* for the channel callback */
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.
56 static bool in_hand_shake
= true;
59 * This state maintains the version number registered by the daemon.
61 static int dm_reg_value
;
63 static void kvp_send_key(struct work_struct
*dummy
);
66 static void kvp_respond_to_host(struct hv_kvp_msg
*msg
, int error
);
67 static void kvp_work_func(struct work_struct
*dummy
);
68 static void kvp_register(int);
70 static DECLARE_DELAYED_WORK(kvp_work
, kvp_work_func
);
71 static DECLARE_WORK(kvp_sendkey_work
, kvp_send_key
);
73 static struct cb_id kvp_id
= { CN_KVP_IDX
, CN_KVP_VAL
};
74 static const char kvp_name
[] = "kvp_kernel_module";
75 static u8
*recv_buffer
;
77 * Register the kernel component with the user-level daemon.
78 * As part of this registration, pass the LIC version number.
82 kvp_register(int reg_value
)
86 struct hv_kvp_msg
*kvp_msg
;
89 msg
= kzalloc(sizeof(*msg
) + sizeof(struct hv_kvp_msg
), GFP_ATOMIC
);
92 kvp_msg
= (struct hv_kvp_msg
*)msg
->data
;
93 version
= kvp_msg
->body
.kvp_register
.version
;
94 msg
->id
.idx
= CN_KVP_IDX
;
95 msg
->id
.val
= CN_KVP_VAL
;
97 kvp_msg
->kvp_hdr
.operation
= reg_value
;
98 strcpy(version
, HV_DRV_VERSION
);
99 msg
->len
= sizeof(struct hv_kvp_msg
);
100 cn_netlink_send(msg
, 0, GFP_ATOMIC
);
105 kvp_work_func(struct work_struct
*dummy
)
108 * If the timer fires, the user-mode component has not responded;
109 * process the pending transaction.
111 kvp_respond_to_host(NULL
, HV_E_FAIL
);
114 static int kvp_handle_handshake(struct hv_kvp_msg
*msg
)
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");
124 case KVP_OP_REGISTER1
:
125 dm_reg_value
= KVP_OP_REGISTER1
;
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
);
136 * We have a compatible daemon; complete the handshake.
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
);
149 * Callback when data is received from user mode.
153 kvp_cn_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
155 struct hv_kvp_msg
*message
;
156 struct hv_kvp_msg_enumerate
*data
;
159 message
= (struct hv_kvp_msg
*)msg
->data
;
162 * If we are negotiating the version information
163 * with the daemon; handle that first.
167 if (kvp_handle_handshake(message
))
168 in_hand_shake
= false;
173 * Based on the version of the daemon, we propagate errors from the
174 * daemon differently.
177 data
= &message
->body
.kvp_enum_data
;
179 switch (dm_reg_value
) {
180 case KVP_OP_REGISTER
:
182 * Null string is used to pass back error condition.
184 if (data
->data
.key
[0] == 0)
188 case KVP_OP_REGISTER1
:
190 * We use the message header information from
191 * the user level daemon to transmit errors.
193 error
= message
->error
;
198 * Complete the transaction by forwarding the key value
199 * to the host. But first, cancel the timeout.
201 if (cancel_delayed_work_sync(&kvp_work
))
202 kvp_respond_to_host(message
, error
);
206 static int process_ob_ipinfo(void *in_msg
, void *out_msg
, int op
)
208 struct hv_kvp_msg
*in
= in_msg
;
209 struct hv_kvp_ip_msg
*out
= out_msg
;
213 case KVP_OP_GET_IP_INFO
:
215 * Transform all parameters into utf16 encoding.
217 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.ip_addr
,
218 strlen((char *)in
->body
.kvp_ip_val
.ip_addr
),
220 (wchar_t *)out
->kvp_ip_val
.ip_addr
,
225 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.sub_net
,
226 strlen((char *)in
->body
.kvp_ip_val
.sub_net
),
228 (wchar_t *)out
->kvp_ip_val
.sub_net
,
233 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.gate_way
,
234 strlen((char *)in
->body
.kvp_ip_val
.gate_way
),
236 (wchar_t *)out
->kvp_ip_val
.gate_way
,
241 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.dns_addr
,
242 strlen((char *)in
->body
.kvp_ip_val
.dns_addr
),
244 (wchar_t *)out
->kvp_ip_val
.dns_addr
,
249 len
= utf8s_to_utf16s((char *)in
->body
.kvp_ip_val
.adapter_id
,
250 strlen((char *)in
->body
.kvp_ip_val
.adapter_id
),
252 (wchar_t *)out
->kvp_ip_val
.adapter_id
,
257 out
->kvp_ip_val
.dhcp_enabled
=
258 in
->body
.kvp_ip_val
.dhcp_enabled
;
259 out
->kvp_ip_val
.addr_family
=
260 in
->body
.kvp_ip_val
.addr_family
;
266 static void process_ib_ipinfo(void *in_msg
, void *out_msg
, int op
)
268 struct hv_kvp_ip_msg
*in
= in_msg
;
269 struct hv_kvp_msg
*out
= out_msg
;
272 case KVP_OP_SET_IP_INFO
:
274 * Transform all parameters into utf8 encoding.
276 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.ip_addr
,
279 (__u8
*)out
->body
.kvp_ip_val
.ip_addr
,
282 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.sub_net
,
285 (__u8
*)out
->body
.kvp_ip_val
.sub_net
,
288 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.gate_way
,
291 (__u8
*)out
->body
.kvp_ip_val
.gate_way
,
294 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.dns_addr
,
297 (__u8
*)out
->body
.kvp_ip_val
.dns_addr
,
300 out
->body
.kvp_ip_val
.dhcp_enabled
= in
->kvp_ip_val
.dhcp_enabled
;
303 utf16s_to_utf8s((wchar_t *)in
->kvp_ip_val
.adapter_id
,
306 (__u8
*)out
->body
.kvp_ip_val
.adapter_id
,
307 MAX_ADAPTER_ID_SIZE
);
309 out
->body
.kvp_ip_val
.addr_family
= in
->kvp_ip_val
.addr_family
;
317 kvp_send_key(struct work_struct
*dummy
)
320 struct hv_kvp_msg
*message
;
321 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
;
327 msg
= kzalloc(sizeof(*msg
) + sizeof(struct hv_kvp_msg
) , GFP_ATOMIC
);
331 msg
->id
.idx
= CN_KVP_IDX
;
332 msg
->id
.val
= CN_KVP_VAL
;
334 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
;
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!
350 switch (message
->kvp_hdr
.operation
) {
351 case KVP_OP_SET_IP_INFO
:
352 process_ib_ipinfo(in_msg
, message
, KVP_OP_SET_IP_INFO
);
354 case KVP_OP_GET_IP_INFO
:
355 process_ib_ipinfo(in_msg
, message
, KVP_OP_GET_IP_INFO
);
358 switch (in_msg
->body
.kvp_set
.data
.value_type
) {
361 * The value is a string - utf16 encoding.
363 message
->body
.kvp_set
.data
.value_size
=
365 (wchar_t *)in_msg
->body
.kvp_set
.data
.value
,
366 in_msg
->body
.kvp_set
.data
.value_size
,
368 message
->body
.kvp_set
.data
.value
,
369 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
- 1) + 1;
374 * The value is a 32 bit scalar.
375 * We save this as a utf8 string.
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
,
385 * The value is a 64 bit scalar.
386 * We save this as a utf8 string.
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
,
396 message
->body
.kvp_set
.data
.key_size
=
398 (wchar_t *)in_msg
->body
.kvp_set
.data
.key
,
399 in_msg
->body
.kvp_set
.data
.key_size
,
401 message
->body
.kvp_set
.data
.key
,
402 HV_KVP_EXCHANGE_MAX_KEY_SIZE
- 1) + 1;
406 message
->body
.kvp_delete
.key_size
=
408 (wchar_t *)in_msg
->body
.kvp_delete
.key
,
409 in_msg
->body
.kvp_delete
.key_size
,
411 message
->body
.kvp_delete
.key
,
412 HV_KVP_EXCHANGE_MAX_KEY_SIZE
- 1) + 1;
415 case KVP_OP_ENUMERATE
:
416 message
->body
.kvp_enum_data
.index
=
417 in_msg
->body
.kvp_enum_data
.index
;
421 msg
->len
= sizeof(struct hv_kvp_msg
);
422 cn_netlink_send(msg
, 0, GFP_ATOMIC
);
429 * Send a response back to the host.
433 kvp_respond_to_host(struct hv_kvp_msg
*msg_to_host
, int error
)
435 struct hv_kvp_msg
*kvp_msg
;
436 struct hv_kvp_exchg_msg_value
*kvp_data
;
439 struct icmsg_hdr
*icmsghdrp
;
443 struct vmbus_channel
*channel
;
448 * If a transaction is not active; log and return.
451 if (!kvp_transaction
.active
) {
453 * This is a spurious call!
455 pr_warn("KVP: Transaction not active\n");
459 * Copy the global state for completing the transaction. Note that
460 * only one transaction can be active at a time.
463 buf_len
= kvp_transaction
.recv_len
;
464 channel
= kvp_transaction
.recv_channel
;
465 req_id
= kvp_transaction
.recv_req_id
;
467 kvp_transaction
.active
= false;
469 icmsghdrp
= (struct icmsg_hdr
*)
470 &recv_buffer
[sizeof(struct vmbuspipe_hdr
)];
472 if (channel
->onchannel_callback
== NULL
)
474 * We have raced with util driver being unloaded;
479 icmsghdrp
->status
= error
;
482 * If the error parameter is set, terminate the host's enumeration
487 * Something failed or we have timedout;
488 * terminate the current host-side iteration.
493 kvp_msg
= (struct hv_kvp_msg
*)
494 &recv_buffer
[sizeof(struct vmbuspipe_hdr
) +
495 sizeof(struct icmsg_hdr
)];
497 switch (kvp_transaction
.kvp_msg
->kvp_hdr
.operation
) {
498 case KVP_OP_GET_IP_INFO
:
499 ret
= process_ob_ipinfo(msg_to_host
,
500 (struct hv_kvp_ip_msg
*)kvp_msg
,
503 icmsghdrp
->status
= HV_E_FAIL
;
506 case KVP_OP_SET_IP_INFO
:
509 kvp_data
= &kvp_msg
->body
.kvp_get
.data
;
520 kvp_data
= &kvp_msg
->body
.kvp_enum_data
.data
;
521 key_name
= msg_to_host
->body
.kvp_enum_data
.data
.key
;
524 * The windows host expects the key/value pair to be encoded
525 * 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).
529 keylen
= utf8s_to_utf16s(key_name
, strlen(key_name
), UTF16_HOST_ENDIAN
,
530 (wchar_t *) kvp_data
->key
,
531 (HV_KVP_EXCHANGE_MAX_KEY_SIZE
/ 2) - 2);
532 kvp_data
->key_size
= 2*(keylen
+ 1); /* utf16 encoding */
535 value
= msg_to_host
->body
.kvp_enum_data
.data
.value
;
536 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 */
542 * If the utf8s to utf16s conversion failed; notify host
545 if ((keylen
< 0) || (valuelen
< 0))
546 icmsghdrp
->status
= HV_E_FAIL
;
548 kvp_data
->value_type
= REG_SZ
; /* all our values are strings */
551 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
553 vmbus_sendpacket(channel
, recv_buffer
, buf_len
, req_id
,
554 VM_PKT_DATA_INBAND
, 0);
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.
568 void hv_kvp_onchannelcallback(void *context
)
570 struct vmbus_channel
*channel
= context
;
574 struct hv_kvp_msg
*kvp_msg
;
576 struct icmsg_hdr
*icmsghdrp
;
577 struct icmsg_negotiate
*negop
= NULL
;
579 if (kvp_transaction
.active
) {
581 * We will defer processing this callback once
582 * the current transaction is complete.
584 kvp_transaction
.kvp_context
= context
;
588 vmbus_recvpacket(channel
, recv_buffer
, PAGE_SIZE
* 2, &recvlen
,
592 icmsghdrp
= (struct icmsg_hdr
*)&recv_buffer
[
593 sizeof(struct vmbuspipe_hdr
)];
595 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
596 vmbus_prep_negotiate_resp(icmsghdrp
, negop
,
597 recv_buffer
, MAX_SRV_VER
, MAX_SRV_VER
);
599 kvp_msg
= (struct hv_kvp_msg
*)&recv_buffer
[
600 sizeof(struct vmbuspipe_hdr
) +
601 sizeof(struct icmsg_hdr
)];
604 * Stash away this global state for completing the
605 * transaction; note transactions are serialized.
608 kvp_transaction
.recv_len
= recvlen
;
609 kvp_transaction
.recv_channel
= channel
;
610 kvp_transaction
.recv_req_id
= requestid
;
611 kvp_transaction
.active
= true;
612 kvp_transaction
.kvp_msg
= kvp_msg
;
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.
623 schedule_work(&kvp_sendkey_work
);
624 schedule_delayed_work(&kvp_work
, 5*HZ
);
630 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
631 | ICMSGHDRFLAG_RESPONSE
;
633 vmbus_sendpacket(channel
, recv_buffer
,
635 VM_PKT_DATA_INBAND
, 0);
641 hv_kvp_init(struct hv_util_service
*srv
)
645 err
= cn_add_callback(&kvp_id
, kvp_name
, kvp_cn_callback
);
648 recv_buffer
= srv
->recv_buffer
;
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
656 kvp_transaction
.active
= true;
661 void hv_kvp_deinit(void)
663 cn_del_callback(&kvp_id
);
664 cancel_delayed_work_sync(&kvp_work
);
665 cancel_work_sync(&kvp_sendkey_work
);