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 int index; /* current index */
46 struct vmbus_channel *recv_channel; /* chn we got the request */
47 u64 recv_req_id; /* request ID. */
50 static void kvp_send_key(struct work_struct *dummy);
52 #define TIMEOUT_FIRED 1
54 static void kvp_respond_to_host(char *key, char *value, int error);
55 static void kvp_work_func(struct work_struct *dummy);
56 static void kvp_register(void);
58 static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
59 static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
61 static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
62 static const char kvp_name[] = "kvp_kernel_module";
63 static u8 *recv_buffer;
65 * Register the kernel component with the user-level daemon.
66 * As part of this registration, pass the LIC version number.
74 struct hv_kvp_msg *kvp_msg;
77 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
80 kvp_msg = (struct hv_kvp_msg *)msg->data;
81 version = kvp_msg->body.kvp_version;
82 msg->id.idx = CN_KVP_IDX;
83 msg->id.val = CN_KVP_VAL;
85 kvp_msg->kvp_hdr.operation = KVP_OP_REGISTER;
86 strcpy(version, HV_DRV_VERSION);
87 msg->len = sizeof(struct hv_kvp_msg);
88 cn_netlink_send(msg, 0, GFP_ATOMIC);
93 kvp_work_func(struct work_struct *dummy)
96 * If the timer fires, the user-mode component has not responded;
97 * process the pending transaction.
99 kvp_respond_to_host("Unknown key", "Guest timed out", TIMEOUT_FIRED);
103 * Callback when data is received from user mode.
107 kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
109 struct hv_kvp_msg *message;
110 struct hv_kvp_msg_enumerate *data;
112 message = (struct hv_kvp_msg *)msg->data;
113 if (message->kvp_hdr.operation == KVP_OP_REGISTER) {
114 pr_info("KVP: user-mode registering done.\n");
118 if (message->kvp_hdr.operation == KVP_OP_ENUMERATE) {
119 data = &message->body.kvp_enum_data;
121 * Complete the transaction by forwarding the key value
122 * to the host. But first, cancel the timeout.
124 if (cancel_delayed_work_sync(&kvp_work))
125 kvp_respond_to_host(data->data.key, data->data.value,
126 !strlen(data->data.key));
131 kvp_send_key(struct work_struct *dummy)
134 struct hv_kvp_msg *message;
135 int index = kvp_transaction.index;
137 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
140 msg->id.idx = CN_KVP_IDX;
141 msg->id.val = CN_KVP_VAL;
143 message = (struct hv_kvp_msg *)msg->data;
144 message->kvp_hdr.operation = KVP_OP_ENUMERATE;
145 message->body.kvp_enum_data.index = index;
146 msg->len = sizeof(struct hv_kvp_msg);
147 cn_netlink_send(msg, 0, GFP_ATOMIC);
154 * Send a response back to the host.
158 kvp_respond_to_host(char *key, char *value, int error)
160 struct hv_kvp_msg *kvp_msg;
161 struct hv_kvp_msg_enumerate *kvp_data;
163 struct icmsg_hdr *icmsghdrp;
164 int keylen, valuelen;
166 struct vmbus_channel *channel;
170 * If a transaction is not active; log and return.
173 if (!kvp_transaction.active) {
175 * This is a spurious call!
177 pr_warn("KVP: Transaction not active\n");
181 * Copy the global state for completing the transaction. Note that
182 * only one transaction can be active at a time.
185 buf_len = kvp_transaction.recv_len;
186 channel = kvp_transaction.recv_channel;
187 req_id = kvp_transaction.recv_req_id;
189 kvp_transaction.active = false;
191 if (channel->onchannel_callback == NULL)
193 * We have raced with util driver being unloaded;
198 icmsghdrp = (struct icmsg_hdr *)
199 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
200 kvp_msg = (struct hv_kvp_msg *)
201 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
202 sizeof(struct icmsg_hdr)];
203 kvp_data = &kvp_msg->body.kvp_enum_data;
207 * If the error parameter is set, terminate the host's enumeration.
211 * We don't support this index or the we have timedout;
212 * terminate the host-side iteration by returning an error.
214 icmsghdrp->status = HV_E_FAIL;
219 * The windows host expects the key/value pair to be encoded
222 keylen = utf8s_to_utf16s(key_name, strlen(key_name),
223 (wchar_t *)kvp_data->data.key);
224 kvp_data->data.key_size = 2*(keylen + 1); /* utf16 encoding */
225 valuelen = utf8s_to_utf16s(value, strlen(value),
226 (wchar_t *)kvp_data->data.value);
227 kvp_data->data.value_size = 2*(valuelen + 1); /* utf16 encoding */
229 kvp_data->data.value_type = REG_SZ; /* all our values are strings */
230 icmsghdrp->status = HV_S_OK;
233 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
235 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
236 VM_PKT_DATA_INBAND, 0);
241 * This callback is invoked when we get a KVP message from the host.
242 * The host ensures that only one KVP transaction can be active at a time.
243 * KVP implementation in Linux needs to forward the key to a user-mde
244 * component to retrive the corresponding value. Consequently, we cannot
245 * respond to the host in the conext of this callback. Since the host
246 * guarantees that at most only one transaction can be active at a time,
247 * we stash away the transaction state in a set of global variables.
250 void hv_kvp_onchannelcallback(void *context)
252 struct vmbus_channel *channel = context;
256 struct hv_kvp_msg *kvp_msg;
257 struct hv_kvp_msg_enumerate *kvp_data;
259 struct icmsg_hdr *icmsghdrp;
260 struct icmsg_negotiate *negop = NULL;
263 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
266 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
267 sizeof(struct vmbuspipe_hdr)];
269 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
270 vmbus_prep_negotiate_resp(icmsghdrp, negop, recv_buffer);
272 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
273 sizeof(struct vmbuspipe_hdr) +
274 sizeof(struct icmsg_hdr)];
276 kvp_data = &kvp_msg->body.kvp_enum_data;
279 * We only support the "get" operation on
280 * "KVP_POOL_AUTO" pool.
283 if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) ||
284 (kvp_msg->kvp_hdr.operation !=
286 icmsghdrp->status = HV_E_FAIL;
291 * Stash away this global state for completing the
292 * transaction; note transactions are serialized.
294 kvp_transaction.recv_len = recvlen;
295 kvp_transaction.recv_channel = channel;
296 kvp_transaction.recv_req_id = requestid;
297 kvp_transaction.active = true;
298 kvp_transaction.index = kvp_data->index;
301 * Get the information from the
302 * user-mode component.
303 * component. This transaction will be
304 * completed when we get the value from
305 * the user-mode component.
306 * Set a timeout to deal with
307 * user-mode not responding.
309 schedule_work(&kvp_sendkey_work);
310 schedule_delayed_work(&kvp_work, 5*HZ);
318 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
319 | ICMSGHDRFLAG_RESPONSE;
321 vmbus_sendpacket(channel, recv_buffer,
323 VM_PKT_DATA_INBAND, 0);
329 hv_kvp_init(struct hv_util_service *srv)
333 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
336 recv_buffer = srv->recv_buffer;
341 void hv_kvp_deinit(void)
343 cn_del_callback(&kvp_id);
344 cancel_delayed_work_sync(&kvp_work);
345 cancel_work_sync(&kvp_sendkey_work);