blob: c71a1486544d39ff07b94aa7e1c2fefce84e457a [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>
29
30#include "logging.h"
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080031#include "hv_api.h"
Ky Srinivasan245ba562010-12-16 18:54:16 -070032#include "vmbus.h"
33#include "vmbus_packet_format.h"
34#include "vmbus_channel_interface.h"
35#include "version_info.h"
36#include "channel.h"
37#include "vmbus_private.h"
38#include "vmbus_api.h"
39#include "utils.h"
40#include "hv_kvp.h"
41
42
43
44/*
45 * Global state maintained for transaction that is being processed.
46 * Note that only one transaction can be active at any point in time.
47 *
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.
51 */
52
53static struct {
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. */
58} kvp_transaction;
59
60static int kvp_send_key(int index);
61
62static void kvp_respond_to_host(char *key, char *value, int error);
63static void kvp_work_func(struct work_struct *dummy);
64static void kvp_register(void);
65
66static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
67
68static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
69static const char kvp_name[] = "kvp_kernel_module";
70static int timeout_fired;
71static u8 *recv_buffer;
72/*
73 * Register the kernel component with the user-level daemon.
74 * As part of this registration, pass the LIC version number.
75 */
76
77static void
78kvp_register(void)
79{
80
81 struct cn_msg *msg;
82
83 msg = kzalloc(sizeof(*msg) + strlen(HV_DRV_VERSION) + 1 , GFP_ATOMIC);
84
85 if (msg) {
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);
92 kfree(msg);
93 }
94}
95static void
96kvp_work_func(struct work_struct *dummy)
97{
98 /*
99 * If the timer fires, the user-mode component has not responded;
100 * process the pending transaction.
101 */
102 kvp_respond_to_host("Unknown key", "Guest timed out", timeout_fired);
103 timeout_fired = 1;
104}
105
106/*
107 * Callback when data is received from user mode.
108 */
109
110static void
111kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
112{
113 struct hv_ku_msg *message;
114
115 message = (struct hv_ku_msg *)msg->data;
116 if (msg->seq == KVP_REGISTER) {
Hank Janssenaf7a5b62011-03-29 13:58:49 -0700117 pr_info("KVP: user-mode registering done.\n");
Ky Srinivasan245ba562010-12-16 18:54:16 -0700118 kvp_register();
119 }
120
121 if (msg->seq == KVP_USER_SET) {
122 /*
123 * Complete the transaction by forwarding the key value
124 * to the host. But first, cancel the timeout.
125 */
126 if (cancel_delayed_work_sync(&kvp_work))
127 kvp_respond_to_host(message->kvp_key,
128 message->kvp_value,
129 !strlen(message->kvp_key));
130 }
131}
132
133static int
134kvp_send_key(int index)
135{
136 struct cn_msg *msg;
137
138 msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
139
140 if (msg) {
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);
147 kfree(msg);
148 return 0;
149 }
150 return 1;
151}
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
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;
195 key_name = key;
196
197 /*
198 * If the error parameter is set, terminate the host's enumeration.
199 */
200 if (error) {
201 /*
202 * We don't support this index or the we have timedout;
203 * terminate the host-side iteration by returning an error.
204 */
205 icmsghdrp->status = HV_E_FAIL;
206 goto response_done;
207 }
208
209 /*
210 * The windows host expects the key/value pair to be encoded
211 * in utf16.
212 */
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 */
219
220 kvp_data->data.value_type = REG_SZ; /* all our values are strings */
221 icmsghdrp->status = HV_S_OK;
222
223response_done:
224 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
225
226 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800227 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700228
229 kvp_transaction.active = false;
230}
231
232/*
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.
240 */
241
242void hv_kvp_onchannelcallback(void *context)
243{
244 struct vmbus_channel *channel = context;
245 u32 recvlen;
246 u64 requestid;
247
248 struct hv_kvp_msg *kvp_msg;
249 struct hv_kvp_msg_enumerate *kvp_data;
250
251 struct icmsg_hdr *icmsghdrp;
252 struct icmsg_negotiate *negop = NULL;
253
254
255 if (kvp_transaction.active)
256 return;
257
258
259 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
260
261 if (recvlen > 0) {
Ky Srinivasan245ba562010-12-16 18:54:16 -0700262 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
263 sizeof(struct vmbuspipe_hdr)];
264
265 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
266 prep_negotiate_resp(icmsghdrp, negop, recv_buffer);
267 } else {
268 kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
269 sizeof(struct vmbuspipe_hdr) +
270 sizeof(struct icmsg_hdr)];
271
272 kvp_data = &kvp_msg->kvp_data;
273
274 /*
275 * We only support the "get" operation on
276 * "KVP_POOL_AUTO" pool.
277 */
278
279 if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) ||
280 (kvp_msg->kvp_hdr.operation !=
281 KVP_OP_ENUMERATE)) {
282 icmsghdrp->status = HV_E_FAIL;
283 goto callback_done;
284 }
285
286 /*
287 * Stash away this global state for completing the
288 * transaction; note transactions are serialized.
289 */
290 kvp_transaction.recv_len = recvlen;
291 kvp_transaction.recv_channel = channel;
292 kvp_transaction.recv_req_id = requestid;
293 kvp_transaction.active = true;
294
295 /*
296 * Get the information from the
297 * user-mode component.
298 * component. This transaction will be
299 * completed when we get the value from
300 * the user-mode component.
301 * Set a timeout to deal with
302 * user-mode not responding.
303 */
304 kvp_send_key(kvp_data->index);
305 schedule_delayed_work(&kvp_work, 100);
306
307 return;
308
309 }
310
311callback_done:
312
313 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
314 | ICMSGHDRFLAG_RESPONSE;
315
316 vmbus_sendpacket(channel, recv_buffer,
317 recvlen, requestid,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800318 VM_PKT_DATA_INBAND, 0);
Ky Srinivasan245ba562010-12-16 18:54:16 -0700319 }
320
321}
322
323int
324hv_kvp_init(void)
325{
326 int err;
327
328 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
329 if (err)
330 return err;
331 recv_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
332 if (!recv_buffer)
333 return -ENOMEM;
334
335 return 0;
336}
337
338void hv_kvp_deinit(void)
339{
340 cn_del_callback(&kvp_id);
341 cancel_delayed_work_sync(&kvp_work);
342 kfree(recv_buffer);
343}