1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/net.h>
26#include <linux/nls.h>
27#include <linux/connector.h>
28#include <linux/workqueue.h>
29
30#include "hyperv.h"
31#include "hv_kvp.h"
32
33
34
35
36
37
38
39
40
41
42
43
44static struct {
45 bool active;
46 int recv_len;
47 struct vmbus_channel *recv_channel;
48 u64 recv_req_id;
49} kvp_transaction;
50
51static int kvp_send_key(int index);
52
53static void kvp_respond_to_host(char *key, char *value, int error);
54static void kvp_work_func(struct work_struct *dummy);
55static void kvp_register(void);
56
57static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
58
59static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
60static const char kvp_name[] = "kvp_kernel_module";
61static int timeout_fired;
62static u8 *recv_buffer;
63
64
65
66
67
68static void
69kvp_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}
86static void
87kvp_work_func(struct work_struct *dummy)
88{
89
90
91
92
93 kvp_respond_to_host("Unknown key", "Guest timed out", timeout_fired);
94 timeout_fired = 1;
95}
96
97
98
99
100
101static void
102kvp_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) {
108 pr_info("KVP: user-mode registering done.\n");
109 kvp_register();
110 }
111
112 if (msg->seq == KVP_USER_SET) {
113
114
115
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
124static int
125kvp_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
146
147
148static void
149kvp_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
162
163
164 if (!kvp_transaction.active) {
165
166
167
168 pr_warn("KVP: Transaction not active\n");
169 return;
170 }
171
172
173
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
190
191 if (error) {
192
193
194
195
196 icmsghdrp->status = HV_E_FAIL;
197 goto response_done;
198 }
199
200
201
202
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);
207 valuelen = utf8s_to_utf16s(value, strlen(value),
208 (wchar_t *)kvp_data->data.value);
209 kvp_data->data.value_size = 2*(valuelen + 1);
210
211 kvp_data->data.value_type = REG_SZ;
212 icmsghdrp->status = HV_S_OK;
213
214response_done:
215 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
216
217 vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
218 VM_PKT_DATA_INBAND, 0);
219
220 kvp_transaction.active = false;
221}
222
223
224
225
226
227
228
229
230
231
232
233void 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) {
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
267
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
279
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
288
289
290
291
292
293
294
295 kvp_send_key(kvp_data->index);
296 schedule_delayed_work(&kvp_work, 100);
297
298 return;
299
300 }
301
302callback_done:
303
304 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
305 | ICMSGHDRFLAG_RESPONSE;
306
307 vmbus_sendpacket(channel, recv_buffer,
308 recvlen, requestid,
309 VM_PKT_DATA_INBAND, 0);
310 }
311
312}
313
314int
315hv_kvp_init(void)
316{
317 int err;
318
319 err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
320 if (err)
321 return err;
322 recv_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
323 if (!recv_buffer)
324 return -ENOMEM;
325
326 return 0;
327}
328
329void hv_kvp_deinit(void)
330{
331 cn_del_callback(&kvp_id);
332 cancel_delayed_work_sync(&kvp_work);
333 kfree(recv_buffer);
334}
335