hv_kvp.c revision 301021
1/*-
2 * Copyright (c) 2014,2016 Microsoft Corp.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27/*
28 *	Author:	Sainath Varanasi.
29 *	Date:	4/2012
30 *	Email:	bsdic@microsoft.com
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/hyperv/utilities/hv_kvp.c 301021 2016-05-31 05:43:59Z sephe $");
35
36#include <sys/param.h>
37#include <sys/kernel.h>
38#include <sys/conf.h>
39#include <sys/uio.h>
40#include <sys/bus.h>
41#include <sys/malloc.h>
42#include <sys/mbuf.h>
43#include <sys/module.h>
44#include <sys/reboot.h>
45#include <sys/lock.h>
46#include <sys/taskqueue.h>
47#include <sys/selinfo.h>
48#include <sys/sysctl.h>
49#include <sys/poll.h>
50#include <sys/proc.h>
51#include <sys/kthread.h>
52#include <sys/syscallsubr.h>
53#include <sys/sysproto.h>
54#include <sys/un.h>
55#include <sys/endian.h>
56#include <sys/_null.h>
57#include <sys/signal.h>
58#include <sys/syslog.h>
59#include <sys/systm.h>
60#include <sys/mutex.h>
61#include <net/if_arp.h>
62
63#include <dev/hyperv/include/hyperv.h>
64#include <dev/hyperv/netvsc/hv_net_vsc.h>
65
66#include "hv_util.h"
67#include "unicode.h"
68#include "hv_kvp.h"
69
70/* hv_kvp defines */
71#define BUFFERSIZE	sizeof(struct hv_kvp_msg)
72#define KVP_SUCCESS	0
73#define KVP_ERROR	1
74#define kvp_hdr		hdr.kvp_hdr
75
76/* hv_kvp debug control */
77static int hv_kvp_log = 0;
78
79#define	hv_kvp_log_error(...)	do {				\
80	if (hv_kvp_log > 0)				\
81		log(LOG_ERR, "hv_kvp: " __VA_ARGS__);	\
82} while (0)
83
84#define	hv_kvp_log_info(...) do {				\
85	if (hv_kvp_log > 1)				\
86		log(LOG_INFO, "hv_kvp: " __VA_ARGS__);		\
87} while (0)
88
89static hv_guid service_guid = { .data =
90	{0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
91	0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3,  0xe6} };
92
93/* character device prototypes */
94static d_open_t		hv_kvp_dev_open;
95static d_close_t	hv_kvp_dev_close;
96static d_read_t		hv_kvp_dev_daemon_read;
97static d_write_t	hv_kvp_dev_daemon_write;
98static d_poll_t		hv_kvp_dev_daemon_poll;
99
100/* hv_kvp character device structure */
101static struct cdevsw hv_kvp_cdevsw =
102{
103	.d_version	= D_VERSION,
104	.d_open		= hv_kvp_dev_open,
105	.d_close	= hv_kvp_dev_close,
106	.d_read		= hv_kvp_dev_daemon_read,
107	.d_write	= hv_kvp_dev_daemon_write,
108	.d_poll		= hv_kvp_dev_daemon_poll,
109	.d_name		= "hv_kvp_dev",
110};
111
112
113/*
114 * Global state to track and synchronize multiple
115 * KVP transaction requests from the host.
116 */
117typedef struct hv_kvp_sc {
118	struct hv_util_sc	util_sc;
119
120	/* Unless specified the pending mutex should be
121	 * used to alter the values of the following parameters:
122	 * 1. req_in_progress
123	 * 2. req_timed_out
124	 */
125	struct mtx		pending_mutex;
126
127	struct task		task;
128
129	/* To track if transaction is active or not */
130	boolean_t		req_in_progress;
131	/* Tracks if daemon did not reply back in time */
132	boolean_t		req_timed_out;
133	/* Tracks if daemon is serving a request currently */
134	boolean_t		daemon_busy;
135
136	/* Length of host message */
137	uint32_t		host_msg_len;
138
139	/* Host message id */
140	uint64_t		host_msg_id;
141
142	/* Current kvp message from the host */
143	struct hv_kvp_msg	*host_kvp_msg;
144
145	 /* Current kvp message for daemon */
146	struct hv_kvp_msg	daemon_kvp_msg;
147
148	/* Rcv buffer for communicating with the host*/
149	uint8_t			*rcv_buf;
150
151	/* Device semaphore to control communication */
152	struct sema		dev_sema;
153
154	/* Indicates if daemon registered with driver */
155	boolean_t		register_done;
156
157	/* Character device status */
158	boolean_t		dev_accessed;
159
160	struct cdev *hv_kvp_dev;
161
162	struct proc *daemon_task;
163
164	struct selinfo hv_kvp_selinfo;
165} hv_kvp_sc;
166
167/* hv_kvp prototypes */
168static int	hv_kvp_req_in_progress(hv_kvp_sc *sc);
169static void	hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t, uint64_t, uint8_t *);
170static void	hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc);
171static void	hv_kvp_process_request(void *context, int pending);
172
173/*
174 * hv_kvp low level functions
175 */
176
177/*
178 * Check if kvp transaction is in progres
179 */
180static int
181hv_kvp_req_in_progress(hv_kvp_sc *sc)
182{
183
184	return (sc->req_in_progress);
185}
186
187
188/*
189 * This routine is called whenever a message is received from the host
190 */
191static void
192hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t rcv_len,
193			uint64_t request_id, uint8_t *rcv_buf)
194{
195
196	/* Store all the relevant message details in the global structure */
197	/* Do not need to use mutex for req_in_progress here */
198	sc->req_in_progress = true;
199	sc->host_msg_len = rcv_len;
200	sc->host_msg_id = request_id;
201	sc->rcv_buf = rcv_buf;
202	sc->host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
203		sizeof(struct hv_vmbus_pipe_hdr) +
204		sizeof(struct hv_vmbus_icmsg_hdr)];
205}
206
207
208/*
209 * hv_kvp - version neogtiation function
210 */
211static void
212hv_kvp_negotiate_version(struct hv_vmbus_icmsg_hdr *icmsghdrp,
213			 struct hv_vmbus_icmsg_negotiate *negop,
214			 uint8_t *buf)
215{
216	int icframe_vercnt;
217	int icmsg_vercnt;
218
219	icmsghdrp->icmsgsize = 0x10;
220
221	negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
222		sizeof(struct hv_vmbus_pipe_hdr) +
223		sizeof(struct hv_vmbus_icmsg_hdr)];
224	icframe_vercnt = negop->icframe_vercnt;
225	icmsg_vercnt = negop->icmsg_vercnt;
226
227	/*
228	 * Select the framework version number we will support
229	 */
230	if ((icframe_vercnt >= 2) && (negop->icversion_data[1].major == 3)) {
231		icframe_vercnt = 3;
232		if (icmsg_vercnt > 2)
233			icmsg_vercnt = 4;
234		else
235			icmsg_vercnt = 3;
236	} else {
237		icframe_vercnt = 1;
238		icmsg_vercnt = 1;
239	}
240
241	negop->icframe_vercnt = 1;
242	negop->icmsg_vercnt = 1;
243	negop->icversion_data[0].major = icframe_vercnt;
244	negop->icversion_data[0].minor = 0;
245	negop->icversion_data[1].major = icmsg_vercnt;
246	negop->icversion_data[1].minor = 0;
247}
248
249
250/*
251 * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
252 */
253static int
254hv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
255				    struct hv_kvp_ip_msg *host_ip_msg)
256{
257	int err_ip, err_subnet, err_gway, err_dns, err_adap;
258	int UNUSED_FLAG = 1;
259
260	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
261	    MAX_IP_ADDR_SIZE,
262	    (char *)umsg->body.kvp_ip_val.ip_addr,
263	    strlen((char *)umsg->body.kvp_ip_val.ip_addr),
264	    UNUSED_FLAG,
265	    &err_ip);
266	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
267	    MAX_IP_ADDR_SIZE,
268	    (char *)umsg->body.kvp_ip_val.sub_net,
269	    strlen((char *)umsg->body.kvp_ip_val.sub_net),
270	    UNUSED_FLAG,
271	    &err_subnet);
272	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
273	    MAX_GATEWAY_SIZE,
274	    (char *)umsg->body.kvp_ip_val.gate_way,
275	    strlen((char *)umsg->body.kvp_ip_val.gate_way),
276	    UNUSED_FLAG,
277	    &err_gway);
278	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
279	    MAX_IP_ADDR_SIZE,
280	    (char *)umsg->body.kvp_ip_val.dns_addr,
281	    strlen((char *)umsg->body.kvp_ip_val.dns_addr),
282	    UNUSED_FLAG,
283	    &err_dns);
284	utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
285	    MAX_IP_ADDR_SIZE,
286	    (char *)umsg->body.kvp_ip_val.adapter_id,
287	    strlen((char *)umsg->body.kvp_ip_val.adapter_id),
288	    UNUSED_FLAG,
289	    &err_adap);
290
291	host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
292	host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
293
294	return (err_ip | err_subnet | err_gway | err_dns | err_adap);
295}
296
297
298/*
299 * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
300 */
301static int
302hv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
303				    struct hv_kvp_msg *umsg)
304{
305	int err_ip, err_subnet, err_gway, err_dns, err_adap;
306	int UNUSED_FLAG = 1;
307	struct hv_device *hv_dev;       /* GUID Data Structure */
308	hn_softc_t *sc;                 /* hn softc structure  */
309	char if_name[4];
310	char buf[HYPERV_GUID_STRLEN];
311
312	device_t *devs;
313	int devcnt;
314
315	/* IP Address */
316	utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
317	    MAX_IP_ADDR_SIZE,
318	    (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
319	    MAX_IP_ADDR_SIZE,
320	    UNUSED_FLAG,
321	    &err_ip);
322
323	/* Adapter ID : GUID */
324	utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
325	    MAX_ADAPTER_ID_SIZE,
326	    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
327	    MAX_ADAPTER_ID_SIZE,
328	    UNUSED_FLAG,
329	    &err_adap);
330
331	if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
332		for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
333			sc = device_get_softc(devs[devcnt]);
334
335			/* Trying to find GUID of Network Device */
336			hv_dev = sc->hn_dev_obj;
337
338			hyperv_guid2str(&hv_dev->device_id, buf, sizeof(buf));
339			sprintf(if_name, "%s%d", "hn", device_get_unit(devs[devcnt]));
340
341			if (strncmp(buf, (char *)umsg->body.kvp_ip_val.adapter_id,
342			    HYPERV_GUID_STRLEN - 1) == 0) {
343				strcpy((char *)umsg->body.kvp_ip_val.adapter_id, if_name);
344				break;
345			}
346		}
347		free(devs, M_TEMP);
348	}
349
350	/* Address Family , DHCP , SUBNET, Gateway, DNS */
351	umsg->kvp_hdr.operation = host_ip_msg->operation;
352	umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
353	umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
354	utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
355	    (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
356	    MAX_IP_ADDR_SIZE,
357	    UNUSED_FLAG,
358	    &err_subnet);
359
360	utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
361	    (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
362	    MAX_GATEWAY_SIZE,
363	    UNUSED_FLAG,
364	    &err_gway);
365
366	utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
367	    (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
368	    MAX_IP_ADDR_SIZE,
369	    UNUSED_FLAG,
370	    &err_dns);
371
372	return (err_ip | err_subnet | err_gway | err_dns | err_adap);
373}
374
375
376/*
377 * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
378 * Ensure utf16_utf8 takes care of the additional string terminating char!!
379 */
380static void
381hv_kvp_convert_hostmsg_to_usermsg(struct hv_kvp_msg *hmsg, struct hv_kvp_msg *umsg)
382{
383	int utf_err = 0;
384	uint32_t value_type;
385	struct hv_kvp_ip_msg *host_ip_msg;
386
387	host_ip_msg = (struct hv_kvp_ip_msg*)hmsg;
388	memset(umsg, 0, sizeof(struct hv_kvp_msg));
389
390	umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
391	umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
392
393	switch (umsg->kvp_hdr.operation) {
394	case HV_KVP_OP_SET_IP_INFO:
395		hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
396		break;
397
398	case HV_KVP_OP_GET_IP_INFO:
399		utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
400		    MAX_ADAPTER_ID_SIZE,
401		    (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
402		    MAX_ADAPTER_ID_SIZE, 1, &utf_err);
403
404		umsg->body.kvp_ip_val.addr_family =
405		    host_ip_msg->kvp_ip_val.addr_family;
406		break;
407
408	case HV_KVP_OP_SET:
409		value_type = hmsg->body.kvp_set.data.value_type;
410
411		switch (value_type) {
412		case HV_REG_SZ:
413			umsg->body.kvp_set.data.value_size =
414			    utf16_to_utf8(
415				(char *)umsg->body.kvp_set.data.msg_value.value,
416				HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
417				(uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
418				hmsg->body.kvp_set.data.value_size,
419				1, &utf_err);
420			/* utf8 encoding */
421			umsg->body.kvp_set.data.value_size =
422			    umsg->body.kvp_set.data.value_size / 2;
423			break;
424
425		case HV_REG_U32:
426			umsg->body.kvp_set.data.value_size =
427			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
428				hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
429			break;
430
431		case HV_REG_U64:
432			umsg->body.kvp_set.data.value_size =
433			    sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
434				(unsigned long long)
435				hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
436			break;
437		}
438
439		umsg->body.kvp_set.data.key_size =
440		    utf16_to_utf8(
441			umsg->body.kvp_set.data.key,
442			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
443			(uint16_t *)hmsg->body.kvp_set.data.key,
444			hmsg->body.kvp_set.data.key_size,
445			1, &utf_err);
446
447		/* utf8 encoding */
448		umsg->body.kvp_set.data.key_size =
449		    umsg->body.kvp_set.data.key_size / 2;
450		break;
451
452	case HV_KVP_OP_GET:
453		umsg->body.kvp_get.data.key_size =
454		    utf16_to_utf8(umsg->body.kvp_get.data.key,
455			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
456			(uint16_t *)hmsg->body.kvp_get.data.key,
457			hmsg->body.kvp_get.data.key_size,
458			1, &utf_err);
459		/* utf8 encoding */
460		umsg->body.kvp_get.data.key_size =
461		    umsg->body.kvp_get.data.key_size / 2;
462		break;
463
464	case HV_KVP_OP_DELETE:
465		umsg->body.kvp_delete.key_size =
466		    utf16_to_utf8(umsg->body.kvp_delete.key,
467			HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
468			(uint16_t *)hmsg->body.kvp_delete.key,
469			hmsg->body.kvp_delete.key_size,
470			1, &utf_err);
471		/* utf8 encoding */
472		umsg->body.kvp_delete.key_size =
473		    umsg->body.kvp_delete.key_size / 2;
474		break;
475
476	case HV_KVP_OP_ENUMERATE:
477		umsg->body.kvp_enum_data.index =
478		    hmsg->body.kvp_enum_data.index;
479		break;
480
481	default:
482		hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
483		    __func__, umsg->kvp_hdr.operation);
484	}
485}
486
487
488/*
489 * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
490 */
491static int
492hv_kvp_convert_usermsg_to_hostmsg(struct hv_kvp_msg *umsg, struct hv_kvp_msg *hmsg)
493{
494	int hkey_len = 0, hvalue_len = 0, utf_err = 0;
495	struct hv_kvp_exchg_msg_value *host_exchg_data;
496	char *key_name, *value;
497
498	struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
499
500	switch (hmsg->kvp_hdr.operation) {
501	case HV_KVP_OP_GET_IP_INFO:
502		return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
503
504	case HV_KVP_OP_SET_IP_INFO:
505	case HV_KVP_OP_SET:
506	case HV_KVP_OP_DELETE:
507		return (KVP_SUCCESS);
508
509	case HV_KVP_OP_ENUMERATE:
510		host_exchg_data = &hmsg->body.kvp_enum_data.data;
511		key_name = umsg->body.kvp_enum_data.data.key;
512		hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
513				((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
514				key_name, strlen(key_name),
515				1, &utf_err);
516		/* utf16 encoding */
517		host_exchg_data->key_size = 2 * (hkey_len + 1);
518		value = umsg->body.kvp_enum_data.data.msg_value.value;
519		hvalue_len = utf8_to_utf16(
520				(uint16_t *)host_exchg_data->msg_value.value,
521				((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
522				value, strlen(value),
523				1, &utf_err);
524		host_exchg_data->value_size = 2 * (hvalue_len + 1);
525		host_exchg_data->value_type = HV_REG_SZ;
526
527		if ((hkey_len < 0) || (hvalue_len < 0))
528			return (HV_KVP_E_FAIL);
529
530		return (KVP_SUCCESS);
531
532	case HV_KVP_OP_GET:
533		host_exchg_data = &hmsg->body.kvp_get.data;
534		value = umsg->body.kvp_get.data.msg_value.value;
535		hvalue_len = utf8_to_utf16(
536				(uint16_t *)host_exchg_data->msg_value.value,
537				((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
538				value, strlen(value),
539				1, &utf_err);
540		/* Convert value size to uft16 */
541		host_exchg_data->value_size = 2 * (hvalue_len + 1);
542		/* Use values by string */
543		host_exchg_data->value_type = HV_REG_SZ;
544
545		if ((hkey_len < 0) || (hvalue_len < 0))
546			return (HV_KVP_E_FAIL);
547
548		return (KVP_SUCCESS);
549
550	default:
551		return (HV_KVP_E_FAIL);
552	}
553}
554
555
556/*
557 * Send the response back to the host.
558 */
559static void
560hv_kvp_respond_host(hv_kvp_sc *sc, int error)
561{
562	struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
563
564	hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
565	    &sc->rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
566
567	if (error)
568		error = HV_KVP_E_FAIL;
569
570	hv_icmsg_hdrp->status = error;
571	hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION | HV_ICMSGHDRFLAG_RESPONSE;
572
573	error = hv_vmbus_channel_send_packet(sc->util_sc.hv_dev->channel,
574			sc->rcv_buf,
575			sc->host_msg_len, sc->host_msg_id,
576			HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
577
578	if (error)
579		hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
580			__func__, error);
581}
582
583
584/*
585 * This is the main kvp kernel process that interacts with both user daemon
586 * and the host
587 */
588static void
589hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc)
590{
591	struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
592	struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
593
594	/* Prepare kvp_msg to be sent to user */
595	hv_kvp_convert_hostmsg_to_usermsg(hmsg, umsg);
596
597	/* Send the msg to user via function deamon_read - setting sema */
598	sema_post(&sc->dev_sema);
599
600	/* We should wake up the daemon, in case it's doing poll() */
601	selwakeup(&sc->hv_kvp_selinfo);
602}
603
604
605/*
606 * Function to read the kvp request buffer from host
607 * and interact with daemon
608 */
609static void
610hv_kvp_process_request(void *context, int pending)
611{
612	uint8_t *kvp_buf;
613	hv_vmbus_channel *channel;
614	uint32_t recvlen = 0;
615	uint64_t requestid;
616	struct hv_vmbus_icmsg_hdr *icmsghdrp;
617	int ret = 0;
618	hv_kvp_sc		*sc;
619
620	hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
621
622	sc = (hv_kvp_sc*)context;
623	kvp_buf = sc->util_sc.receive_buffer;
624	channel = sc->util_sc.hv_dev->channel;
625
626	ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
627		&recvlen, &requestid);
628
629	while ((ret == 0) && (recvlen > 0)) {
630
631		icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
632			&kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
633
634		hv_kvp_transaction_init(sc, recvlen, requestid, kvp_buf);
635		if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
636			hv_kvp_negotiate_version(icmsghdrp, NULL, kvp_buf);
637			hv_kvp_respond_host(sc, ret);
638
639			/*
640			 * It is ok to not acquire the mutex before setting
641			 * req_in_progress here because negotiation is the
642			 * first thing that happens and hence there is no
643			 * chance of a race condition.
644			 */
645
646			sc->req_in_progress = false;
647			hv_kvp_log_info("%s :version negotiated\n", __func__);
648
649		} else {
650			if (!sc->daemon_busy) {
651
652				hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
653				mtx_lock(&sc->pending_mutex);
654				sc->req_timed_out = false;
655				sc->daemon_busy = true;
656				mtx_unlock(&sc->pending_mutex);
657
658				hv_kvp_send_msg_to_daemon(sc);
659				hv_kvp_log_info("%s: waiting for daemon\n", __func__);
660			}
661
662			/* Wait 5 seconds for daemon to respond back */
663			tsleep(sc, 0, "kvpworkitem", 5 * hz);
664			hv_kvp_log_info("%s: came out of wait\n", __func__);
665		}
666
667		mtx_lock(&sc->pending_mutex);
668
669		/* Notice that once req_timed_out is set to true
670		 * it will remain true until the next request is
671		 * sent to the daemon. The response from daemon
672		 * is forwarded to host only when this flag is
673		 * false.
674		 */
675		sc->req_timed_out = true;
676
677		/*
678		 * Cancel request if so need be.
679		 */
680		if (hv_kvp_req_in_progress(sc)) {
681			hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
682			hv_kvp_respond_host(sc, HV_KVP_E_FAIL);
683			sc->req_in_progress = false;
684		}
685
686		mtx_unlock(&sc->pending_mutex);
687
688		/*
689		 * Try reading next buffer
690		 */
691		recvlen = 0;
692		ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
693			&recvlen, &requestid);
694		hv_kvp_log_info("%s: read: context %p, ret =%d, recvlen=%d\n",
695			__func__, context, ret, recvlen);
696	}
697}
698
699
700/*
701 * Callback routine that gets called whenever there is a message from host
702 */
703static void
704hv_kvp_callback(void *context)
705{
706	hv_kvp_sc *sc = (hv_kvp_sc*)context;
707	/*
708	 The first request from host will not be handled until daemon is registered.
709	 when callback is triggered without a registered daemon, callback just return.
710	 When a new daemon gets regsitered, this callbcak is trigged from _write op.
711	*/
712	if (sc->register_done) {
713		hv_kvp_log_info("%s: Queuing work item\n", __func__);
714		taskqueue_enqueue(taskqueue_thread, &sc->task);
715	}
716}
717
718static int
719hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
720				struct thread *td)
721{
722	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
723
724	hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
725	if (sc->dev_accessed)
726		return (-EBUSY);
727
728	sc->daemon_task = curproc;
729	sc->dev_accessed = true;
730	sc->daemon_busy = false;
731	return (0);
732}
733
734
735static int
736hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
737				 struct thread *td __unused)
738{
739	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
740
741	hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
742	sc->dev_accessed = false;
743	sc->register_done = false;
744	return (0);
745}
746
747
748/*
749 * hv_kvp_daemon read invokes this function
750 * acts as a send to daemon
751 */
752static int
753hv_kvp_dev_daemon_read(struct cdev *dev, struct uio *uio, int ioflag __unused)
754{
755	size_t amt;
756	int error = 0;
757	struct hv_kvp_msg *hv_kvp_dev_buf;
758	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
759
760	/* Check hv_kvp daemon registration status*/
761	if (!sc->register_done)
762		return (KVP_ERROR);
763
764	sema_wait(&sc->dev_sema);
765
766	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
767	memcpy(hv_kvp_dev_buf, &sc->daemon_kvp_msg, sizeof(struct hv_kvp_msg));
768
769	amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
770		BUFFERSIZE + 1 - uio->uio_offset);
771
772	if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
773		hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
774
775	free(hv_kvp_dev_buf, M_TEMP);
776	return (error);
777}
778
779
780/*
781 * hv_kvp_daemon write invokes this function
782 * acts as a receive from daemon
783 */
784static int
785hv_kvp_dev_daemon_write(struct cdev *dev, struct uio *uio, int ioflag __unused)
786{
787	size_t amt;
788	int error = 0;
789	struct hv_kvp_msg *hv_kvp_dev_buf;
790	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
791
792	uio->uio_offset = 0;
793	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
794
795	amt = MIN(uio->uio_resid, BUFFERSIZE);
796	error = uiomove(hv_kvp_dev_buf, amt, uio);
797
798	if (error != 0) {
799		free(hv_kvp_dev_buf, M_TEMP);
800		return (error);
801	}
802	memcpy(&sc->daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
803
804	free(hv_kvp_dev_buf, M_TEMP);
805	if (sc->register_done == false) {
806		if (sc->daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
807			sc->register_done = true;
808			hv_kvp_callback(dev->si_drv1);
809		}
810		else {
811			hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
812			return (KVP_ERROR);
813		}
814	} else {
815
816		mtx_lock(&sc->pending_mutex);
817
818		if(!sc->req_timed_out) {
819			struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
820			struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
821
822			hv_kvp_convert_usermsg_to_hostmsg(umsg, hmsg);
823			hv_kvp_respond_host(sc, KVP_SUCCESS);
824			wakeup(sc);
825			sc->req_in_progress = false;
826		}
827
828		sc->daemon_busy = false;
829		mtx_unlock(&sc->pending_mutex);
830	}
831
832	return (error);
833}
834
835
836/*
837 * hv_kvp_daemon poll invokes this function to check if data is available
838 * for daemon to read.
839 */
840static int
841hv_kvp_dev_daemon_poll(struct cdev *dev, int events, struct thread *td)
842{
843	int revents = 0;
844	hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
845
846	mtx_lock(&sc->pending_mutex);
847	/*
848	 * We check global flag daemon_busy for the data availiability for
849	 * userland to read. Deamon_busy is set to true before driver has data
850	 * for daemon to read. It is set to false after daemon sends
851	 * then response back to driver.
852	 */
853	if (sc->daemon_busy == true)
854		revents = POLLIN;
855	else
856		selrecord(td, &sc->hv_kvp_selinfo);
857
858	mtx_unlock(&sc->pending_mutex);
859
860	return (revents);
861}
862
863static int
864hv_kvp_probe(device_t dev)
865{
866	const char *p = vmbus_get_type(dev);
867
868	if (resource_disabled("hvkvp", 0))
869		return ENXIO;
870
871	if (!memcmp(p, &service_guid, sizeof(hv_guid))) {
872		device_set_desc(dev, "Hyper-V KVP Service");
873		return BUS_PROBE_DEFAULT;
874	}
875
876	return ENXIO;
877}
878
879static int
880hv_kvp_attach(device_t dev)
881{
882	int error;
883	struct sysctl_oid_list *child;
884	struct sysctl_ctx_list *ctx;
885
886	hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
887
888	sc->util_sc.callback = hv_kvp_callback;
889	sema_init(&sc->dev_sema, 0, "hv_kvp device semaphore");
890	mtx_init(&sc->pending_mutex, "hv-kvp pending mutex",
891		NULL, MTX_DEF);
892
893	ctx = device_get_sysctl_ctx(dev);
894	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
895
896	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "hv_kvp_log",
897	    CTLFLAG_RW, &hv_kvp_log, 0, "Hyperv KVP service log level");
898
899	TASK_INIT(&sc->task, 0, hv_kvp_process_request, sc);
900
901	/* create character device */
902	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
903			&sc->hv_kvp_dev,
904			&hv_kvp_cdevsw,
905			0,
906			UID_ROOT,
907			GID_WHEEL,
908			0640,
909			"hv_kvp_dev");
910
911	if (error != 0)
912		return (error);
913	sc->hv_kvp_dev->si_drv1 = sc;
914
915	return hv_util_attach(dev);
916}
917
918static int
919hv_kvp_detach(device_t dev)
920{
921	hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
922
923	if (sc->daemon_task != NULL) {
924		PROC_LOCK(sc->daemon_task);
925		kern_psignal(sc->daemon_task, SIGKILL);
926		PROC_UNLOCK(sc->daemon_task);
927	}
928
929	destroy_dev(sc->hv_kvp_dev);
930	return hv_util_detach(dev);
931}
932
933static device_method_t kvp_methods[] = {
934	/* Device interface */
935	DEVMETHOD(device_probe, hv_kvp_probe),
936	DEVMETHOD(device_attach, hv_kvp_attach),
937	DEVMETHOD(device_detach, hv_kvp_detach),
938	{ 0, 0 }
939};
940
941static driver_t kvp_driver = { "hvkvp", kvp_methods, sizeof(hv_kvp_sc)};
942
943static devclass_t kvp_devclass;
944
945DRIVER_MODULE(hv_kvp, vmbus, kvp_driver, kvp_devclass, NULL, NULL);
946MODULE_VERSION(hv_kvp, 1);
947MODULE_DEPEND(hv_kvp, vmbus, 1, 1, 1);
948