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>
36 * Global state maintained for transaction that is being processed.
37 * Note that only one transaction can be active at any point in time.
39 * This state is set when we receive a request from the host; we
40 * cleanup this state when the transaction is completed - when we respond
41 * to the host with the key value.
45 bool active
; /* transaction status - active or not */
46 int recv_len
; /* number of bytes received. */
47 struct vmbus_channel
*recv_channel
; /* chn we got the request */
48 u64 recv_req_id
; /* request ID. */
51 static int kvp_send_key(int index
);
53 static void kvp_respond_to_host(char *key
, char *value
, int error
);
54 static void kvp_work_func(struct work_struct
*dummy
);
55 static void kvp_register(void);
57 static DECLARE_DELAYED_WORK(kvp_work
, kvp_work_func
);
59 static struct cb_id kvp_id
= { CN_KVP_IDX
, CN_KVP_VAL
};
60 static const char kvp_name
[] = "kvp_kernel_module";
61 static int timeout_fired
;
62 static u8
*recv_buffer
;
64 * Register the kernel component with the user-level daemon.
65 * As part of this registration, pass the LIC version number.
74 msg
= kzalloc(sizeof(*msg
) + strlen(HV_DRV_VERSION
) + 1 , GFP_ATOMIC
);
77 msg
->id
.idx
= CN_KVP_IDX
;
78 msg
->id
.val
= CN_KVP_VAL
;
79 msg
->seq
= KVP_REGISTER
;
80 strcpy(msg
->data
, HV_DRV_VERSION
);
81 msg
->len
= strlen(HV_DRV_VERSION
) + 1;
82 cn_netlink_send(msg
, 0, GFP_ATOMIC
);
87 kvp_work_func(struct work_struct
*dummy
)
90 * If the timer fires, the user-mode component has not responded;
91 * process the pending transaction.
93 kvp_respond_to_host("Unknown key", "Guest timed out", timeout_fired
);
98 * Callback when data is received from user mode.
102 kvp_cn_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
104 struct hv_ku_msg
*message
;
106 message
= (struct hv_ku_msg
*)msg
->data
;
107 if (msg
->seq
== KVP_REGISTER
) {
108 pr_info("KVP: user-mode registering done.\n");
112 if (msg
->seq
== KVP_USER_SET
) {
114 * Complete the transaction by forwarding the key value
115 * to the host. But first, cancel the timeout.
117 if (cancel_delayed_work_sync(&kvp_work
))
118 kvp_respond_to_host(message
->kvp_key
,
120 !strlen(message
->kvp_key
));
125 kvp_send_key(int index
)
129 msg
= kzalloc(sizeof(*msg
) + sizeof(struct hv_kvp_msg
) , GFP_ATOMIC
);
132 msg
->id
.idx
= CN_KVP_IDX
;
133 msg
->id
.val
= CN_KVP_VAL
;
134 msg
->seq
= KVP_KERNEL_GET
;
135 ((struct hv_ku_msg
*)msg
->data
)->kvp_index
= index
;
136 msg
->len
= sizeof(struct hv_ku_msg
);
137 cn_netlink_send(msg
, 0, GFP_ATOMIC
);
145 * Send a response back to the host.
149 kvp_respond_to_host(char *key
, char *value
, int error
)
151 struct hv_kvp_msg
*kvp_msg
;
152 struct hv_kvp_msg_enumerate
*kvp_data
;
154 struct icmsg_hdr
*icmsghdrp
;
155 int keylen
, valuelen
;
157 struct vmbus_channel
*channel
;
161 * If a transaction is not active; log and return.
164 if (!kvp_transaction
.active
) {
166 * This is a spurious call!
168 pr_warn("KVP: Transaction not active\n");
172 * Copy the global state for completing the transaction. Note that
173 * only one transaction can be active at a time.
176 buf_len
= kvp_transaction
.recv_len
;
177 channel
= kvp_transaction
.recv_channel
;
178 req_id
= kvp_transaction
.recv_req_id
;
180 icmsghdrp
= (struct icmsg_hdr
*)
181 &recv_buffer
[sizeof(struct vmbuspipe_hdr
)];
182 kvp_msg
= (struct hv_kvp_msg
*)
183 &recv_buffer
[sizeof(struct vmbuspipe_hdr
) +
184 sizeof(struct icmsg_hdr
)];
185 kvp_data
= &kvp_msg
->kvp_data
;
189 * If the error parameter is set, terminate the host's enumeration.
193 * We don't support this index or the we have timedout;
194 * terminate the host-side iteration by returning an error.
196 icmsghdrp
->status
= HV_E_FAIL
;
201 * The windows host expects the key/value pair to be encoded
204 keylen
= utf8s_to_utf16s(key_name
, strlen(key_name
),
205 (wchar_t *)kvp_data
->data
.key
);
206 kvp_data
->data
.key_size
= 2*(keylen
+ 1); /* utf16 encoding */
207 valuelen
= utf8s_to_utf16s(value
, strlen(value
),
208 (wchar_t *)kvp_data
->data
.value
);
209 kvp_data
->data
.value_size
= 2*(valuelen
+ 1); /* utf16 encoding */
211 kvp_data
->data
.value_type
= REG_SZ
; /* all our values are strings */
212 icmsghdrp
->status
= HV_S_OK
;
215 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
| ICMSGHDRFLAG_RESPONSE
;
217 vmbus_sendpacket(channel
, recv_buffer
, buf_len
, req_id
,
218 VM_PKT_DATA_INBAND
, 0);
220 kvp_transaction
.active
= false;
224 * This callback is invoked when we get a KVP message from the host.
225 * The host ensures that only one KVP transaction can be active at a time.
226 * KVP implementation in Linux needs to forward the key to a user-mde
227 * component to retrive the corresponding value. Consequently, we cannot
228 * respond to the host in the conext of this callback. Since the host
229 * guarantees that at most only one transaction can be active at a time,
230 * we stash away the transaction state in a set of global variables.
233 void hv_kvp_onchannelcallback(void *context
)
235 struct vmbus_channel
*channel
= context
;
239 struct hv_kvp_msg
*kvp_msg
;
240 struct hv_kvp_msg_enumerate
*kvp_data
;
242 struct icmsg_hdr
*icmsghdrp
;
243 struct icmsg_negotiate
*negop
= NULL
;
246 if (kvp_transaction
.active
)
250 vmbus_recvpacket(channel
, recv_buffer
, PAGE_SIZE
, &recvlen
, &requestid
);
253 icmsghdrp
= (struct icmsg_hdr
*)&recv_buffer
[
254 sizeof(struct vmbuspipe_hdr
)];
256 if (icmsghdrp
->icmsgtype
== ICMSGTYPE_NEGOTIATE
) {
257 prep_negotiate_resp(icmsghdrp
, negop
, recv_buffer
);
259 kvp_msg
= (struct hv_kvp_msg
*)&recv_buffer
[
260 sizeof(struct vmbuspipe_hdr
) +
261 sizeof(struct icmsg_hdr
)];
263 kvp_data
= &kvp_msg
->kvp_data
;
266 * We only support the "get" operation on
267 * "KVP_POOL_AUTO" pool.
270 if ((kvp_msg
->kvp_hdr
.pool
!= KVP_POOL_AUTO
) ||
271 (kvp_msg
->kvp_hdr
.operation
!=
273 icmsghdrp
->status
= HV_E_FAIL
;
278 * Stash away this global state for completing the
279 * transaction; note transactions are serialized.
281 kvp_transaction
.recv_len
= recvlen
;
282 kvp_transaction
.recv_channel
= channel
;
283 kvp_transaction
.recv_req_id
= requestid
;
284 kvp_transaction
.active
= true;
287 * Get the information from the
288 * user-mode component.
289 * component. This transaction will be
290 * completed when we get the value from
291 * the user-mode component.
292 * Set a timeout to deal with
293 * user-mode not responding.
295 kvp_send_key(kvp_data
->index
);
296 schedule_delayed_work(&kvp_work
, 100);
304 icmsghdrp
->icflags
= ICMSGHDRFLAG_TRANSACTION
305 | ICMSGHDRFLAG_RESPONSE
;
307 vmbus_sendpacket(channel
, recv_buffer
,
309 VM_PKT_DATA_INBAND
, 0);
319 err
= cn_add_callback(&kvp_id
, kvp_name
, kvp_cn_callback
);
322 recv_buffer
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
329 void hv_kvp_deinit(void)
331 cn_del_callback(&kvp_id
);
332 cancel_delayed_work_sync(&kvp_work
);