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