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.
25 #include <linux/net.h>
26 #include <linux/nls.h>
27 #include <linux/connector.h>
28 #include <linux/workqueue.h>
33 #include "vmbus_packet_format.h"
34 #include "vmbus_channel_interface.h"
35 #include "version_info.h"
37 #include "vmbus_private.h"
38 #include "vmbus_api.h"
45 * Global state maintained for transaction that is being processed.
46 * Note that only one transaction can be active at any point in time.
48 * This state is set when we receive a request from the host; we
49 * cleanup this state when the transaction is completed - when we respond
50 * to the host with the key value.
54 bool active; /* transaction status - active or not */
55 int recv_len; /* number of bytes received. */
56 struct vmbus_channel *recv_channel; /* chn we got the request */
57 u64 recv_req_id; /* request ID. */
60 static int kvp_send_key(int index);
62 static void kvp_respond_to_host(char *key, char *value, int error);
63 static void kvp_work_func(struct work_struct *dummy);
64 static void kvp_register(void);
66 static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
68 static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
69 static const char kvp_name[] = "kvp_kernel_module";
70 static int timeout_fired;
71 static u8 *recv_buffer;
73 * Register the kernel component with the user-level daemon.
74 * As part of this registration, pass the LIC version number.
83 msg = kzalloc(sizeof(*msg) + strlen(HV_DRV_VERSION) + 1 , GFP_ATOMIC);
86 msg->id.idx = CN_KVP_IDX;
87 msg->id.val = CN_KVP_VAL;
88 msg->seq = KVP_REGISTER;
89 strcpy(msg->data, HV_DRV_VERSION);
90 msg->len = strlen(HV_DRV_VERSION) + 1;
91 cn_netlink_send(msg, 0, GFP_ATOMIC);
96 kvp_work_func(struct work_struct *dummy)
99 * If the timer fires, the user-mode component has not responded;
100 * process the pending transaction.
102 kvp_respond_to_host("Unknown key", "Guest timed out", timeout_fired);
107 * Callback when data is received from user mode.
111 kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
113 struct hv_ku_msg *message;
115 message = (struct hv_ku_msg *)msg->data;
116 if (msg->seq == KVP_REGISTER) {
117 printk(KERN_INFO "KVP: user-mode registering done.\n");
121 if (msg->seq == KVP_USER_SET) {
123 * Complete the transaction by forwarding the key value
124 * to the host. But first, cancel the timeout.
126 if (cancel_delayed_work_sync(&kvp_work))
127 kvp_respond_to_host(message->kvp_key,
129 !strlen(message->kvp_key));
134 kvp_send_key(int index)
138 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
141 msg->id.idx = CN_KVP_IDX;
142 msg->id.val = CN_KVP_VAL;
143 msg->seq = KVP_KERNEL_GET;
144 ((struct hv_ku_msg *)msg->data)->kvp_index = index;
145 msg->len = sizeof(struct hv_ku_msg);
146 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 printk(KERN_WARNING "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 icmsghdrp = (struct icmsg_hdr *)
190 &recv_buffer[sizeof(struct vmbuspipe_hdr)];
191 kvp_msg = (struct hv_kvp_msg *)
192 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
193 sizeof(struct icmsg_hdr)];
194 kvp_data = &kvp_msg->kvp_data;
198 * If the error parameter is set, terminate the host's enumeration.
202 * We don't support this index or the we have timedout;
203 * terminate the host-side iteration by returning an error.
205 icmsghdrp->status = HV_E_FAIL;
210 * The windows host expects the key/value pair to be encoded
213 keylen = utf8s_to_utf16s(key_name, strlen(key_name),
214 (wchar_t *)kvp_data->data.key);
215 kvp_data->data.key_size = 2*(keylen + 1); /* utf16 encoding */
216 valuelen = utf8s_to_utf16s(value, strlen(value),
217 (wchar_t *)kvp_data->data.value);
218 kvp_data->data.value_size = 2*(valuelen + 1); /* utf16 encoding */
220 kvp_data->data.value_type = REG_SZ; /* all our values are strings */
221 icmsghdrp->status = HV_S_OK;
224 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
226 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
227 VM_PKT_DATA_INBAND, 0);
229 kvp_transaction.active = false;
233 * This callback is invoked when we get a KVP message from the host.
234 * The host ensures that only one KVP transaction can be active at a time.
235 * KVP implementation in Linux needs to forward the key to a user-mde
236 * component to retrive the corresponding value. Consequently, we cannot
237 * respond to the host in the conext of this callback. Since the host
238 * guarantees that at most only one transaction can be active at a time,
239 * we stash away the transaction state in a set of global variables.
242 void hv_kvp_onchannelcallback(void *context)
244 struct vmbus_channel *channel = context;
248 struct hv_kvp_msg *kvp_msg;
249 struct hv_kvp_msg_enumerate *kvp_data;
251 struct icmsg_hdr *icmsghdrp;
252 struct icmsg_negotiate *negop = NULL;
255 if (kvp_transaction.active)
259 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
262 DPRINT_DBG(VMBUS, "KVP packet: len=%d, requestid=%lld",
265 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
266 sizeof(struct vmbuspipe_hdr)];
268 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
269 prep_negotiate_resp(icmsghdrp, negop, recv_buffer);
271 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
272 sizeof(struct vmbuspipe_hdr) +
273 sizeof(struct icmsg_hdr)];
275 kvp_data = &kvp_msg->kvp_data;
278 * We only support the "get" operation on
279 * "KVP_POOL_AUTO" pool.
282 if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) ||
283 (kvp_msg->kvp_hdr.operation !=
285 icmsghdrp->status = HV_E_FAIL;
290 * Stash away this global state for completing the
291 * transaction; note transactions are serialized.
293 kvp_transaction.recv_len = recvlen;
294 kvp_transaction.recv_channel = channel;
295 kvp_transaction.recv_req_id = requestid;
296 kvp_transaction.active = true;
299 * Get the information from the
300 * user-mode component.
301 * component. This transaction will be
302 * completed when we get the value from
303 * the user-mode component.
304 * Set a timeout to deal with
305 * user-mode not responding.
307 kvp_send_key(kvp_data->index);
308 schedule_delayed_work(&kvp_work, 100);
316 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
317 | ICMSGHDRFLAG_RESPONSE;
319 vmbus_sendpacket(channel, recv_buffer,
321 VM_PKT_DATA_INBAND, 0);
331 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
334 recv_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
341 void hv_kvp_deinit(void)
343 cn_del_callback(&kvp_id);
344 cancel_delayed_work_sync(&kvp_work);