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>
31 #include "hyperv_vmbus.h"
34 * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
36 #define WS2008_SRV_MAJOR 1
37 #define WS2008_SRV_MINOR 0
38 #define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
40 #define WIN7_SRV_MAJOR 3
41 #define WIN7_SRV_MINOR 0
42 #define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
44 #define WIN8_SRV_MAJOR 4
45 #define WIN8_SRV_MINOR 0
46 #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
49 * Global state maintained for transaction that is being processed.
50 * Note that only one transaction can be active at any point in time.
52 * This state is set when we receive a request from the host; we
53 * cleanup this state when the transaction is completed - when we respond
54 * to the host with the key value.
58 bool active; /* transaction status - active or not */
59 int recv_len; /* number of bytes received. */
60 struct hv_kvp_msg *kvp_msg; /* current message */
61 struct vmbus_channel *recv_channel; /* chn we got the request */
62 u64 recv_req_id; /* request ID. */
63 void *kvp_context; /* for the channel callback */
67 * Before we can accept KVP messages from the host, we need
68 * to handshake with the user level daemon. This state tracks
69 * if we are in the handshake phase.
71 static bool in_hand_shake = true;
74 * This state maintains the version number registered by the daemon.
76 static int dm_reg_value;
78 static void kvp_send_key(struct work_struct *dummy);
81 static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
82 static void kvp_timeout_func(struct work_struct *dummy);
83 static void kvp_register(int);
85 static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func);
86 static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
88 static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
89 static const char kvp_name[] = "kvp_kernel_module";
90 static u8 *recv_buffer;
92 * Register the kernel component with the user-level daemon.
93 * As part of this registration, pass the LIC version number.
94 * This number has no meaning, it satisfies the registration protocol.
96 #define HV_DRV_VERSION "3.1"
99 kvp_register(int reg_value)
103 struct hv_kvp_msg *kvp_msg;
106 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
109 kvp_msg = (struct hv_kvp_msg *)msg->data;
110 version = kvp_msg->body.kvp_register.version;
111 msg->id.idx = CN_KVP_IDX;
112 msg->id.val = CN_KVP_VAL;
114 kvp_msg->kvp_hdr.operation = reg_value;
115 strcpy(version, HV_DRV_VERSION);
116 msg->len = sizeof(struct hv_kvp_msg);
117 cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
122 static void kvp_timeout_func(struct work_struct *dummy)
125 * If the timer fires, the user-mode component has not responded;
126 * process the pending transaction.
128 kvp_respond_to_host(NULL, HV_E_FAIL);
131 static int kvp_handle_handshake(struct hv_kvp_msg *msg)
135 switch (msg->kvp_hdr.operation) {
136 case KVP_OP_REGISTER:
137 dm_reg_value = KVP_OP_REGISTER;
138 pr_info("KVP: IP injection functionality not available\n");
139 pr_info("KVP: Upgrade the KVP daemon\n");
141 case KVP_OP_REGISTER1:
142 dm_reg_value = KVP_OP_REGISTER1;
145 pr_info("KVP: incompatible daemon\n");
146 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
147 KVP_OP_REGISTER1, msg->kvp_hdr.operation);
153 * We have a compatible daemon; complete the handshake.
155 pr_info("KVP: user-mode registering done.\n");
156 kvp_register(dm_reg_value);
157 kvp_transaction.active = false;
158 hv_poll_channel(kvp_transaction.kvp_context,
159 hv_kvp_onchannelcallback);
166 * Callback when data is received from user mode.
170 kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
172 struct hv_kvp_msg *message;
173 struct hv_kvp_msg_enumerate *data;
176 message = (struct hv_kvp_msg *)msg->data;
179 * If we are negotiating the version information
180 * with the daemon; handle that first.
184 if (kvp_handle_handshake(message))
185 in_hand_shake = false;
190 * Based on the version of the daemon, we propagate errors from the
191 * daemon differently.
194 data = &message->body.kvp_enum_data;
196 switch (dm_reg_value) {
197 case KVP_OP_REGISTER:
199 * Null string is used to pass back error condition.
201 if (data->data.key[0] == 0)
205 case KVP_OP_REGISTER1:
207 * We use the message header information from
208 * the user level daemon to transmit errors.
210 error = message->error;
215 * Complete the transaction by forwarding the key value
216 * to the host. But first, cancel the timeout.
218 if (cancel_delayed_work_sync(&kvp_timeout_work))
219 kvp_respond_to_host(message, error);
223 static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
225 struct hv_kvp_msg *in = in_msg;
226 struct hv_kvp_ip_msg *out = out_msg;
230 case KVP_OP_GET_IP_INFO:
232 * Transform all parameters into utf16 encoding.
234 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
235 strlen((char *)in->body.kvp_ip_val.ip_addr),
237 (wchar_t *)out->kvp_ip_val.ip_addr,
242 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
243 strlen((char *)in->body.kvp_ip_val.sub_net),
245 (wchar_t *)out->kvp_ip_val.sub_net,
250 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
251 strlen((char *)in->body.kvp_ip_val.gate_way),
253 (wchar_t *)out->kvp_ip_val.gate_way,
258 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
259 strlen((char *)in->body.kvp_ip_val.dns_addr),
261 (wchar_t *)out->kvp_ip_val.dns_addr,
266 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
267 strlen((char *)in->body.kvp_ip_val.adapter_id),
269 (wchar_t *)out->kvp_ip_val.adapter_id,
274 out->kvp_ip_val.dhcp_enabled =
275 in->body.kvp_ip_val.dhcp_enabled;
276 out->kvp_ip_val.addr_family =
277 in->body.kvp_ip_val.addr_family;
283 static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
285 struct hv_kvp_ip_msg *in = in_msg;
286 struct hv_kvp_msg *out = out_msg;
289 case KVP_OP_SET_IP_INFO:
291 * Transform all parameters into utf8 encoding.
293 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
296 (__u8 *)out->body.kvp_ip_val.ip_addr,
299 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
302 (__u8 *)out->body.kvp_ip_val.sub_net,
305 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
308 (__u8 *)out->body.kvp_ip_val.gate_way,
311 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
314 (__u8 *)out->body.kvp_ip_val.dns_addr,
317 out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
320 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
323 (__u8 *)out->body.kvp_ip_val.adapter_id,
324 MAX_ADAPTER_ID_SIZE);
326 out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
334 kvp_send_key(struct work_struct *dummy)
337 struct hv_kvp_msg *message;
338 struct hv_kvp_msg *in_msg;
339 __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
340 __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
345 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
349 msg->id.idx = CN_KVP_IDX;
350 msg->id.val = CN_KVP_VAL;
352 message = (struct hv_kvp_msg *)msg->data;
353 message->kvp_hdr.operation = operation;
354 message->kvp_hdr.pool = pool;
355 in_msg = kvp_transaction.kvp_msg;
358 * The key/value strings sent from the host are encoded in
359 * in utf16; convert it to utf8 strings.
360 * The host assures us that the utf16 strings will not exceed
361 * the max lengths specified. We will however, reserve room
362 * for the string terminating character - in the utf16s_utf8s()
363 * function we limit the size of the buffer where the converted
364 * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
365 * that the strings can be properly terminated!
368 switch (message->kvp_hdr.operation) {
369 case KVP_OP_SET_IP_INFO:
370 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
372 case KVP_OP_GET_IP_INFO:
373 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
376 switch (in_msg->body.kvp_set.data.value_type) {
379 * The value is a string - utf16 encoding.
381 message->body.kvp_set.data.value_size =
383 (wchar_t *)in_msg->body.kvp_set.data.value,
384 in_msg->body.kvp_set.data.value_size,
386 message->body.kvp_set.data.value,
387 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
392 * The value is a 32 bit scalar.
393 * We save this as a utf8 string.
395 val32 = in_msg->body.kvp_set.data.value_u32;
396 message->body.kvp_set.data.value_size =
397 sprintf(message->body.kvp_set.data.value,
403 * The value is a 64 bit scalar.
404 * We save this as a utf8 string.
406 val64 = in_msg->body.kvp_set.data.value_u64;
407 message->body.kvp_set.data.value_size =
408 sprintf(message->body.kvp_set.data.value,
414 message->body.kvp_set.data.key_size =
416 (wchar_t *)in_msg->body.kvp_set.data.key,
417 in_msg->body.kvp_set.data.key_size,
419 message->body.kvp_set.data.key,
420 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
424 message->body.kvp_delete.key_size =
426 (wchar_t *)in_msg->body.kvp_delete.key,
427 in_msg->body.kvp_delete.key_size,
429 message->body.kvp_delete.key,
430 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
433 case KVP_OP_ENUMERATE:
434 message->body.kvp_enum_data.index =
435 in_msg->body.kvp_enum_data.index;
439 msg->len = sizeof(struct hv_kvp_msg);
440 rc = cn_netlink_send(msg, 0, 0, GFP_ATOMIC);
442 pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
443 if (cancel_delayed_work_sync(&kvp_timeout_work))
444 kvp_respond_to_host(message, HV_E_FAIL);
453 * Send a response back to the host.
457 kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
459 struct hv_kvp_msg *kvp_msg;
460 struct hv_kvp_exchg_msg_value *kvp_data;
463 struct icmsg_hdr *icmsghdrp;
467 struct vmbus_channel *channel;
472 * If a transaction is not active; log and return.
475 if (!kvp_transaction.active) {
477 * This is a spurious call!
479 pr_warn("KVP: Transaction not active\n");
483 * Copy the global state for completing the transaction. Note that
484 * only one transaction can be active at a time.
487 buf_len = kvp_transaction.recv_len;
488 channel = kvp_transaction.recv_channel;
489 req_id = kvp_transaction.recv_req_id;
491 kvp_transaction.active = false;
493 icmsghdrp = (struct icmsg_hdr *)
494 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
496 if (channel->onchannel_callback == NULL)
498 * We have raced with util driver being unloaded;
503 icmsghdrp->status = error;
506 * If the error parameter is set, terminate the host's enumeration
511 * Something failed or we have timedout;
512 * terminate the current host-side iteration.
517 kvp_msg = (struct hv_kvp_msg *)
518 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
519 sizeof(struct icmsg_hdr)];
521 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
522 case KVP_OP_GET_IP_INFO:
523 ret = process_ob_ipinfo(msg_to_host,
524 (struct hv_kvp_ip_msg *)kvp_msg,
527 icmsghdrp->status = HV_E_FAIL;
530 case KVP_OP_SET_IP_INFO:
533 kvp_data = &kvp_msg->body.kvp_get.data;
544 kvp_data = &kvp_msg->body.kvp_enum_data.data;
545 key_name = msg_to_host->body.kvp_enum_data.data.key;
548 * The windows host expects the key/value pair to be encoded
549 * in utf16. Ensure that the key/value size reported to the host
550 * will be less than or equal to the MAX size (including the
551 * terminating character).
553 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
554 (wchar_t *) kvp_data->key,
555 (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
556 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
559 value = msg_to_host->body.kvp_enum_data.data.value;
560 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
561 (wchar_t *) kvp_data->value,
562 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
563 kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
566 * If the utf8s to utf16s conversion failed; notify host
569 if ((keylen < 0) || (valuelen < 0))
570 icmsghdrp->status = HV_E_FAIL;
572 kvp_data->value_type = REG_SZ; /* all our values are strings */
575 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
577 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
578 VM_PKT_DATA_INBAND, 0);
579 hv_poll_channel(channel, hv_kvp_onchannelcallback);
583 * This callback is invoked when we get a KVP message from the host.
584 * The host ensures that only one KVP transaction can be active at a time.
585 * KVP implementation in Linux needs to forward the key to a user-mde
586 * component to retrive the corresponding value. Consequently, we cannot
587 * respond to the host in the conext of this callback. Since the host
588 * guarantees that at most only one transaction can be active at a time,
589 * we stash away the transaction state in a set of global variables.
592 void hv_kvp_onchannelcallback(void *context)
594 struct vmbus_channel *channel = context;
598 struct hv_kvp_msg *kvp_msg;
600 struct icmsg_hdr *icmsghdrp;
601 struct icmsg_negotiate *negop = NULL;
605 if (kvp_transaction.active) {
607 * We will defer processing this callback once
608 * the current transaction is complete.
610 kvp_transaction.kvp_context = context;
613 kvp_transaction.kvp_context = NULL;
615 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
619 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
620 sizeof(struct vmbuspipe_hdr)];
622 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
624 * Based on the host, select appropriate
625 * framework and service versions we will
628 switch (vmbus_proto_version) {
629 case (VERSION_WS2008):
630 util_fw_version = UTIL_WS2K8_FW_VERSION;
631 kvp_srv_version = WS2008_SRV_VERSION;
634 util_fw_version = UTIL_FW_VERSION;
635 kvp_srv_version = WIN7_SRV_VERSION;
638 util_fw_version = UTIL_FW_VERSION;
639 kvp_srv_version = WIN8_SRV_VERSION;
641 vmbus_prep_negotiate_resp(icmsghdrp, negop,
642 recv_buffer, util_fw_version,
646 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
647 sizeof(struct vmbuspipe_hdr) +
648 sizeof(struct icmsg_hdr)];
651 * Stash away this global state for completing the
652 * transaction; note transactions are serialized.
655 kvp_transaction.recv_len = recvlen;
656 kvp_transaction.recv_channel = channel;
657 kvp_transaction.recv_req_id = requestid;
658 kvp_transaction.active = true;
659 kvp_transaction.kvp_msg = kvp_msg;
662 * Get the information from the
663 * user-mode component.
664 * component. This transaction will be
665 * completed when we get the value from
666 * the user-mode component.
667 * Set a timeout to deal with
668 * user-mode not responding.
670 schedule_work(&kvp_sendkey_work);
671 schedule_delayed_work(&kvp_timeout_work, 5*HZ);
677 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
678 | ICMSGHDRFLAG_RESPONSE;
680 vmbus_sendpacket(channel, recv_buffer,
682 VM_PKT_DATA_INBAND, 0);
688 hv_kvp_init(struct hv_util_service *srv)
692 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
695 recv_buffer = srv->recv_buffer;
698 * When this driver loads, the user level daemon that
699 * processes the host requests may not yet be running.
700 * Defer processing channel callbacks until the daemon
703 kvp_transaction.active = true;
708 void hv_kvp_deinit(void)
710 cn_del_callback(&kvp_id);
711 cancel_delayed_work_sync(&kvp_timeout_work);
712 cancel_work_sync(&kvp_sendkey_work);