Commit | Line | Data |
---|---|---|
245ba56a KS |
1 | /* |
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 | */ | |
af7a5b6e | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
245ba56a KS |
24 | |
25 | #include <linux/net.h> | |
26 | #include <linux/nls.h> | |
27 | #include <linux/connector.h> | |
28 | #include <linux/workqueue.h> | |
29 | ||
3f335ea2 | 30 | #include "hyperv.h" |
245ba56a KS |
31 | #include "hv_kvp.h" |
32 | ||
33 | ||
34 | ||
35 | /* | |
36 | * Global state maintained for transaction that is being processed. | |
37 | * Note that only one transaction can be active at any point in time. | |
38 | * | |
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. | |
42 | */ | |
43 | ||
44 | static struct { | |
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. */ | |
49 | } kvp_transaction; | |
50 | ||
51 | static int kvp_send_key(int index); | |
52 | ||
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); | |
56 | ||
57 | static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func); | |
58 | ||
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; | |
63 | /* | |
64 | * Register the kernel component with the user-level daemon. | |
65 | * As part of this registration, pass the LIC version number. | |
66 | */ | |
67 | ||
68 | static void | |
69 | kvp_register(void) | |
70 | { | |
71 | ||
72 | struct cn_msg *msg; | |
73 | ||
74 | msg = kzalloc(sizeof(*msg) + strlen(HV_DRV_VERSION) + 1 , GFP_ATOMIC); | |
75 | ||
76 | if (msg) { | |
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); | |
83 | kfree(msg); | |
84 | } | |
85 | } | |
86 | static void | |
87 | kvp_work_func(struct work_struct *dummy) | |
88 | { | |
89 | /* | |
90 | * If the timer fires, the user-mode component has not responded; | |
91 | * process the pending transaction. | |
92 | */ | |
93 | kvp_respond_to_host("Unknown key", "Guest timed out", timeout_fired); | |
94 | timeout_fired = 1; | |
95 | } | |
96 | ||
97 | /* | |
98 | * Callback when data is received from user mode. | |
99 | */ | |
100 | ||
101 | static void | |
102 | kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) | |
103 | { | |
104 | struct hv_ku_msg *message; | |
105 | ||
106 | message = (struct hv_ku_msg *)msg->data; | |
107 | if (msg->seq == KVP_REGISTER) { | |
af7a5b6e | 108 | pr_info("KVP: user-mode registering done.\n"); |
245ba56a KS |
109 | kvp_register(); |
110 | } | |
111 | ||
112 | if (msg->seq == KVP_USER_SET) { | |
113 | /* | |
114 | * Complete the transaction by forwarding the key value | |
115 | * to the host. But first, cancel the timeout. | |
116 | */ | |
117 | if (cancel_delayed_work_sync(&kvp_work)) | |
118 | kvp_respond_to_host(message->kvp_key, | |
119 | message->kvp_value, | |
120 | !strlen(message->kvp_key)); | |
121 | } | |
122 | } | |
123 | ||
124 | static int | |
125 | kvp_send_key(int index) | |
126 | { | |
127 | struct cn_msg *msg; | |
128 | ||
129 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC); | |
130 | ||
131 | if (msg) { | |
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); | |
138 | kfree(msg); | |
139 | return 0; | |
140 | } | |
141 | return 1; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Send a response back to the host. | |
146 | */ | |
147 | ||
148 | static void | |
149 | kvp_respond_to_host(char *key, char *value, int error) | |
150 | { | |
151 | struct hv_kvp_msg *kvp_msg; | |
152 | struct hv_kvp_msg_enumerate *kvp_data; | |
153 | char *key_name; | |
154 | struct icmsg_hdr *icmsghdrp; | |
155 | int keylen, valuelen; | |
156 | u32 buf_len; | |
157 | struct vmbus_channel *channel; | |
158 | u64 req_id; | |
159 | ||
160 | /* | |
161 | * If a transaction is not active; log and return. | |
162 | */ | |
163 | ||
164 | if (!kvp_transaction.active) { | |
165 | /* | |
166 | * This is a spurious call! | |
167 | */ | |
af7a5b6e | 168 | pr_warn("KVP: Transaction not active\n"); |
245ba56a KS |
169 | return; |
170 | } | |
171 | /* | |
172 | * Copy the global state for completing the transaction. Note that | |
173 | * only one transaction can be active at a time. | |
174 | */ | |
175 | ||
176 | buf_len = kvp_transaction.recv_len; | |
177 | channel = kvp_transaction.recv_channel; | |
178 | req_id = kvp_transaction.recv_req_id; | |
179 | ||
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; | |
186 | key_name = key; | |
187 | ||
188 | /* | |
189 | * If the error parameter is set, terminate the host's enumeration. | |
190 | */ | |
191 | if (error) { | |
192 | /* | |
193 | * We don't support this index or the we have timedout; | |
194 | * terminate the host-side iteration by returning an error. | |
195 | */ | |
196 | icmsghdrp->status = HV_E_FAIL; | |
197 | goto response_done; | |
198 | } | |
199 | ||
200 | /* | |
201 | * The windows host expects the key/value pair to be encoded | |
202 | * in utf16. | |
203 | */ | |
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 */ | |
210 | ||
211 | kvp_data->data.value_type = REG_SZ; /* all our values are strings */ | |
212 | icmsghdrp->status = HV_S_OK; | |
213 | ||
214 | response_done: | |
215 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; | |
216 | ||
217 | vmbus_sendpacket(channel, recv_buffer, buf_len, req_id, | |
415f2287 | 218 | VM_PKT_DATA_INBAND, 0); |
245ba56a KS |
219 | |
220 | kvp_transaction.active = false; | |
221 | } | |
222 | ||
223 | /* | |
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. | |
231 | */ | |
232 | ||
233 | void hv_kvp_onchannelcallback(void *context) | |
234 | { | |
235 | struct vmbus_channel *channel = context; | |
236 | u32 recvlen; | |
237 | u64 requestid; | |
238 | ||
239 | struct hv_kvp_msg *kvp_msg; | |
240 | struct hv_kvp_msg_enumerate *kvp_data; | |
241 | ||
242 | struct icmsg_hdr *icmsghdrp; | |
243 | struct icmsg_negotiate *negop = NULL; | |
244 | ||
245 | ||
246 | if (kvp_transaction.active) | |
247 | return; | |
248 | ||
249 | ||
250 | vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid); | |
251 | ||
252 | if (recvlen > 0) { | |
245ba56a KS |
253 | icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ |
254 | sizeof(struct vmbuspipe_hdr)]; | |
255 | ||
256 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { | |
257 | prep_negotiate_resp(icmsghdrp, negop, recv_buffer); | |
258 | } else { | |
259 | kvp_msg = (struct hv_kvp_msg *)&recv_buffer[ | |
260 | sizeof(struct vmbuspipe_hdr) + | |
261 | sizeof(struct icmsg_hdr)]; | |
262 | ||
263 | kvp_data = &kvp_msg->kvp_data; | |
264 | ||
265 | /* | |
266 | * We only support the "get" operation on | |
267 | * "KVP_POOL_AUTO" pool. | |
268 | */ | |
269 | ||
270 | if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) || | |
271 | (kvp_msg->kvp_hdr.operation != | |
272 | KVP_OP_ENUMERATE)) { | |
273 | icmsghdrp->status = HV_E_FAIL; | |
274 | goto callback_done; | |
275 | } | |
276 | ||
277 | /* | |
278 | * Stash away this global state for completing the | |
279 | * transaction; note transactions are serialized. | |
280 | */ | |
281 | kvp_transaction.recv_len = recvlen; | |
282 | kvp_transaction.recv_channel = channel; | |
283 | kvp_transaction.recv_req_id = requestid; | |
284 | kvp_transaction.active = true; | |
285 | ||
286 | /* | |
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. | |
294 | */ | |
295 | kvp_send_key(kvp_data->index); | |
296 | schedule_delayed_work(&kvp_work, 100); | |
297 | ||
298 | return; | |
299 | ||
300 | } | |
301 | ||
302 | callback_done: | |
303 | ||
304 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | |
305 | | ICMSGHDRFLAG_RESPONSE; | |
306 | ||
307 | vmbus_sendpacket(channel, recv_buffer, | |
308 | recvlen, requestid, | |
415f2287 | 309 | VM_PKT_DATA_INBAND, 0); |
245ba56a KS |
310 | } |
311 | ||
312 | } | |
313 | ||
314 | int | |
a29b643c | 315 | hv_kvp_init(struct hv_util_service *srv) |
245ba56a KS |
316 | { |
317 | int err; | |
318 | ||
319 | err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback); | |
320 | if (err) | |
321 | return err; | |
a29b643c | 322 | recv_buffer = srv->recv_buffer; |
245ba56a KS |
323 | |
324 | return 0; | |
325 | } | |
326 | ||
327 | void hv_kvp_deinit(void) | |
328 | { | |
329 | cn_del_callback(&kvp_id); | |
330 | cancel_delayed_work_sync(&kvp_work); | |
245ba56a | 331 | } |