blob: 0ef4c1f6ca54b5c8325dbc1e36b1a379ae8f4cec [file] [log] [blame]
Ky Srinivasan245ba562010-12-16 18:54:16 -07001/*
2 * An implementation of key value pair (KVP) functionality for Linux.
3 *
4 *
5 * Copyright (C) 2010, Novell, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
7 *
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.
11 *
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
16 * details.
17 *
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.
21 *
22 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Ky Srinivasan245ba562010-12-16 18:54:16 -070024
25#include <linux/net.h>
26#include <linux/nls.h>
27#include <linux/connector.h>
28#include <linux/workqueue.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070029#include <linux/hyperv.h>
Ky Srinivasan245ba562010-12-16 18:54:16 -070030
Ky Srinivasan245ba562010-12-16 18:54:16 -070031
32
33/*
34 * Global state maintained for transaction that is being processed.
35 * Note that only one transaction can be active at any point in time.
36 *
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.
40 */
41
42static struct {
43 bool active; /* transaction status - active or not */
44 int recv_len; /* number of bytes received. */
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070045 int index; /* current index */
Ky Srinivasan245ba562010-12-16 18:54:16 -070046 struct vmbus_channel *recv_channel; /* chn we got the request */
47 u64 recv_req_id; /* request ID. */
48} kvp_transaction;
49
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070050static void kvp_send_key(struct work_struct *dummy);
Ky Srinivasan245ba562010-12-16 18:54:16 -070051
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -070052#define TIMEOUT_FIRED 1
53
Ky Srinivasan245ba562010-12-16 18:54:16 -070054static void kvp_respond_to_host(char *key, char *value, int error);
55static void kvp_work_func(struct work_struct *dummy);
56static void kvp_register(void);
57
58static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -070059static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
Ky Srinivasan245ba562010-12-16 18:54:16 -070060
61static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
62static const char kvp_name[] = "kvp_kernel_module";
Ky Srinivasan245ba562010-12-16 18:54:16 -070063static u8 *recv_buffer;
64/*
65 * Register the kernel component with the user-level daemon.
66 * As part of this registration, pass the LIC version number.
67 */
68
69static void
70kvp_register(void)
71{
72
73 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080074 struct hv_kvp_msg *kvp_msg;
75 char *version;
Ky Srinivasan245ba562010-12-16 18:54:16 -070076
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080077 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
Ky Srinivasan245ba562010-12-16 18:54:16 -070078
79 if (msg) {
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080080 kvp_msg = (struct hv_kvp_msg *)msg->data;
81 version = kvp_msg->body.kvp_version;
Ky Srinivasan245ba562010-12-16 18:54:16 -070082 msg->id.idx = CN_KVP_IDX;
83 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -080084
85 kvp_msg->kvp_hdr.operation = KVP_OP_REGISTER;
86 strcpy(version, HV_DRV_VERSION);
87 msg->len = sizeof(struct hv_kvp_msg);
Ky Srinivasan245ba562010-12-16 18:54:16 -070088 cn_netlink_send(msg, 0, GFP_ATOMIC);
89 kfree(msg);
90 }
91}
92static void
93kvp_work_func(struct work_struct *dummy)
94{
95 /*
96 * If the timer fires, the user-mode component has not responded;
97 * process the pending transaction.
98 */
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -070099 kvp_respond_to_host("Unknown key", "Guest timed out", TIMEOUT_FIRED);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700100}
101
102/*
103 * Callback when data is received from user mode.
104 */
105
106static void
107kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
108{
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800109 struct hv_kvp_msg *message;
110 struct hv_kvp_msg_enumerate *data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700111
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800112 message = (struct hv_kvp_msg *)msg->data;
113 if (message->kvp_hdr.operation == KVP_OP_REGISTER) {
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700114 pr_info("KVP: user-mode registering done.\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700115 kvp_register();
116 }
117
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800118 if (message->kvp_hdr.operation == KVP_OP_ENUMERATE) {
119 data = &message->body.kvp_enum_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700120 /*
121 * Complete the transaction by forwarding the key value
122 * to the host. But first, cancel the timeout.
123 */
124 if (cancel_delayed_work_sync(&kvp_work))
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800125 kvp_respond_to_host(data->data.key, data->data.value,
126 !strlen(data->data.key));
Ky Srinivasan245ba562010-12-16 18:54:16 -0700127 }
128}
129
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700130static void
131kvp_send_key(struct work_struct *dummy)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700132{
133 struct cn_msg *msg;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800134 struct hv_kvp_msg *message;
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700135 int index = kvp_transaction.index;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700136
137 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
138
139 if (msg) {
140 msg->id.idx = CN_KVP_IDX;
141 msg->id.val = CN_KVP_VAL;
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800142
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);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700147 cn_netlink_send(msg, 0, GFP_ATOMIC);
148 kfree(msg);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700149 }
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700150 return;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700151}
152
153/*
154 * Send a response back to the host.
155 */
156
157static void
158kvp_respond_to_host(char *key, char *value, int error)
159{
160 struct hv_kvp_msg *kvp_msg;
161 struct hv_kvp_msg_enumerate *kvp_data;
162 char *key_name;
163 struct icmsg_hdr *icmsghdrp;
164 int keylen, valuelen;
165 u32 buf_len;
166 struct vmbus_channel *channel;
167 u64 req_id;
168
169 /*
170 * If a transaction is not active; log and return.
171 */
172
173 if (!kvp_transaction.active) {
174 /*
175 * This is a spurious call!
176 */
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700177 pr_warn("KVP: Transaction not active\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700178 return;
179 }
180 /*
181 * Copy the global state for completing the transaction. Note that
182 * only one transaction can be active at a time.
183 */
184
185 buf_len = kvp_transaction.recv_len;
186 channel = kvp_transaction.recv_channel;
187 req_id = kvp_transaction.recv_req_id;
188
K. Y. Srinivasan76e5f812011-10-04 14:00:02 -0700189 kvp_transaction.active = false;
190
K. Y. Srinivasan4e65f6e2011-09-18 10:31:34 -0700191 if (channel->onchannel_callback == NULL)
192 /*
193 * We have raced with util driver being unloaded;
194 * silently return.
195 */
196 return;
197
Ky Srinivasan245ba562010-12-16 18:54:16 -0700198 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)];
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800203 kvp_data = &kvp_msg->body.kvp_enum_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700204 key_name = key;
205
206 /*
207 * If the error parameter is set, terminate the host's enumeration.
208 */
209 if (error) {
210 /*
211 * We don't support this index or the we have timedout;
212 * terminate the host-side iteration by returning an error.
213 */
214 icmsghdrp->status = HV_E_FAIL;
215 goto response_done;
216 }
217
218 /*
219 * The windows host expects the key/value pair to be encoded
220 * in utf16.
221 */
Alan Stern0720a062011-11-17 16:42:19 -0500222 keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
223 (wchar_t *) kvp_data->data.key,
224 HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700225 kvp_data->data.key_size = 2*(keylen + 1); /* utf16 encoding */
Alan Stern0720a062011-11-17 16:42:19 -0500226 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
227 (wchar_t *) kvp_data->data.value,
228 HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700229 kvp_data->data.value_size = 2*(valuelen + 1); /* utf16 encoding */
230
231 kvp_data->data.value_type = REG_SZ; /* all our values are strings */
232 icmsghdrp->status = HV_S_OK;
233
234response_done:
235 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
236
237 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800238 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700239
Ky Srinivasan245ba562010-12-16 18:54:16 -0700240}
241
242/*
243 * This callback is invoked when we get a KVP message from the host.
244 * The host ensures that only one KVP transaction can be active at a time.
245 * KVP implementation in Linux needs to forward the key to a user-mde
246 * component to retrive the corresponding value. Consequently, we cannot
247 * respond to the host in the conext of this callback. Since the host
248 * guarantees that at most only one transaction can be active at a time,
249 * we stash away the transaction state in a set of global variables.
250 */
251
252void hv_kvp_onchannelcallback(void *context)
253{
254 struct vmbus_channel *channel = context;
255 u32 recvlen;
256 u64 requestid;
257
258 struct hv_kvp_msg *kvp_msg;
259 struct hv_kvp_msg_enumerate *kvp_data;
260
261 struct icmsg_hdr *icmsghdrp;
262 struct icmsg_negotiate *negop = NULL;
263
264
Ky Srinivasan245ba562010-12-16 18:54:16 -0700265 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
266
267 if (recvlen > 0) {
Ky Srinivasan245ba562010-12-16 18:54:16 -0700268 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
269 sizeof(struct vmbuspipe_hdr)];
270
271 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
Greg Kroah-Hartmanda0e9632011-10-11 08:42:28 -0600272 vmbus_prep_negotiate_resp(icmsghdrp, negop, recv_buffer);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700273 } else {
274 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
275 sizeof(struct vmbuspipe_hdr) +
276 sizeof(struct icmsg_hdr)];
277
K. Y. Srinivasan2640335432012-02-02 16:56:50 -0800278 kvp_data = &kvp_msg->body.kvp_enum_data;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700279
280 /*
281 * We only support the "get" operation on
282 * "KVP_POOL_AUTO" pool.
283 */
284
285 if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) ||
286 (kvp_msg->kvp_hdr.operation !=
287 KVP_OP_ENUMERATE)) {
288 icmsghdrp->status = HV_E_FAIL;
289 goto callback_done;
290 }
291
292 /*
293 * Stash away this global state for completing the
294 * transaction; note transactions are serialized.
295 */
296 kvp_transaction.recv_len = recvlen;
297 kvp_transaction.recv_channel = channel;
298 kvp_transaction.recv_req_id = requestid;
299 kvp_transaction.active = true;
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700300 kvp_transaction.index = kvp_data->index;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700301
302 /*
303 * Get the information from the
304 * user-mode component.
305 * component. This transaction will be
306 * completed when we get the value from
307 * the user-mode component.
308 * Set a timeout to deal with
309 * user-mode not responding.
310 */
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700311 schedule_work(&kvp_sendkey_work);
312 schedule_delayed_work(&kvp_work, 5*HZ);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700313
314 return;
315
316 }
317
318callback_done:
319
320 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
321 | ICMSGHDRFLAG_RESPONSE;
322
323 vmbus_sendpacket(channel, recv_buffer,
324 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800325 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700326 }
327
328}
329
330int
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700331hv_kvp_init(struct hv_util_service *srv)
Ky Srinivasan245ba562010-12-16 18:54:16 -0700332{
333 int err;
334
335 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
336 if (err)
337 return err;
K. Y. Srinivasana29b6432011-09-18 10:31:33 -0700338 recv_buffer = srv->recv_buffer;
Ky Srinivasan245ba562010-12-16 18:54:16 -0700339
340 return 0;
341}
342
343void hv_kvp_deinit(void)
344{
345 cn_del_callback(&kvp_id);
346 cancel_delayed_work_sync(&kvp_work);
K. Y. Srinivasane2bb6532011-10-09 19:42:28 -0700347 cancel_work_sync(&kvp_sendkey_work);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700348}