hv_kvp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/net.h>
  25. #include <linux/nls.h>
  26. #include <linux/connector.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/hyperv.h>
  29. /*
  30. * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7)
  31. */
  32. #define WIN7_SRV_MAJOR 3
  33. #define WIN7_SRV_MINOR 0
  34. #define WIN7_SRV_MAJOR_MINOR (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
  35. #define WIN8_SRV_MAJOR 4
  36. #define WIN8_SRV_MINOR 0
  37. #define WIN8_SRV_MAJOR_MINOR (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
  38. /*
  39. * Global state maintained for transaction that is being processed.
  40. * Note that only one transaction can be active at any point in time.
  41. *
  42. * This state is set when we receive a request from the host; we
  43. * cleanup this state when the transaction is completed - when we respond
  44. * to the host with the key value.
  45. */
  46. static struct {
  47. bool active; /* transaction status - active or not */
  48. int recv_len; /* number of bytes received. */
  49. struct hv_kvp_msg *kvp_msg; /* current message */
  50. struct vmbus_channel *recv_channel; /* chn we got the request */
  51. u64 recv_req_id; /* request ID. */
  52. void *kvp_context; /* for the channel callback */
  53. } kvp_transaction;
  54. /*
  55. * Before we can accept KVP messages from the host, we need
  56. * to handshake with the user level daemon. This state tracks
  57. * if we are in the handshake phase.
  58. */
  59. static bool in_hand_shake = true;
  60. /*
  61. * This state maintains the version number registered by the daemon.
  62. */
  63. static int dm_reg_value;
  64. static void kvp_send_key(struct work_struct *dummy);
  65. static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
  66. static void kvp_work_func(struct work_struct *dummy);
  67. static void kvp_register(int);
  68. static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
  69. static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
  70. static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
  71. static const char kvp_name[] = "kvp_kernel_module";
  72. static u8 *recv_buffer;
  73. /*
  74. * Register the kernel component with the user-level daemon.
  75. * As part of this registration, pass the LIC version number.
  76. * This number has no meaning, it satisfies the registration protocol.
  77. */
  78. #define HV_DRV_VERSION "3.1"
  79. static void
  80. kvp_register(int reg_value)
  81. {
  82. struct cn_msg *msg;
  83. struct hv_kvp_msg *kvp_msg;
  84. char *version;
  85. msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
  86. if (msg) {
  87. kvp_msg = (struct hv_kvp_msg *)msg->data;
  88. version = kvp_msg->body.kvp_register.version;
  89. msg->id.idx = CN_KVP_IDX;
  90. msg->id.val = CN_KVP_VAL;
  91. kvp_msg->kvp_hdr.operation = reg_value;
  92. strcpy(version, HV_DRV_VERSION);
  93. msg->len = sizeof(struct hv_kvp_msg);
  94. cn_netlink_send(msg, 0, GFP_ATOMIC);
  95. kfree(msg);
  96. }
  97. }
  98. static void
  99. kvp_work_func(struct work_struct *dummy)
  100. {
  101. /*
  102. * If the timer fires, the user-mode component has not responded;
  103. * process the pending transaction.
  104. */
  105. kvp_respond_to_host(NULL, HV_E_FAIL);
  106. }
  107. static int kvp_handle_handshake(struct hv_kvp_msg *msg)
  108. {
  109. int ret = 1;
  110. switch (msg->kvp_hdr.operation) {
  111. case KVP_OP_REGISTER:
  112. dm_reg_value = KVP_OP_REGISTER;
  113. pr_info("KVP: IP injection functionality not available\n");
  114. pr_info("KVP: Upgrade the KVP daemon\n");
  115. break;
  116. case KVP_OP_REGISTER1:
  117. dm_reg_value = KVP_OP_REGISTER1;
  118. break;
  119. default:
  120. pr_info("KVP: incompatible daemon\n");
  121. pr_info("KVP: KVP version: %d, Daemon version: %d\n",
  122. KVP_OP_REGISTER1, msg->kvp_hdr.operation);
  123. ret = 0;
  124. }
  125. if (ret) {
  126. /*
  127. * We have a compatible daemon; complete the handshake.
  128. */
  129. pr_info("KVP: user-mode registering done.\n");
  130. kvp_register(dm_reg_value);
  131. kvp_transaction.active = false;
  132. if (kvp_transaction.kvp_context)
  133. hv_kvp_onchannelcallback(kvp_transaction.kvp_context);
  134. }
  135. return ret;
  136. }
  137. /*
  138. * Callback when data is received from user mode.
  139. */
  140. static void
  141. kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
  142. {
  143. struct hv_kvp_msg *message;
  144. struct hv_kvp_msg_enumerate *data;
  145. int error = 0;
  146. message = (struct hv_kvp_msg *)msg->data;
  147. /*
  148. * If we are negotiating the version information
  149. * with the daemon; handle that first.
  150. */
  151. if (in_hand_shake) {
  152. if (kvp_handle_handshake(message))
  153. in_hand_shake = false;
  154. return;
  155. }
  156. /*
  157. * Based on the version of the daemon, we propagate errors from the
  158. * daemon differently.
  159. */
  160. data = &message->body.kvp_enum_data;
  161. switch (dm_reg_value) {
  162. case KVP_OP_REGISTER:
  163. /*
  164. * Null string is used to pass back error condition.
  165. */
  166. if (data->data.key[0] == 0)
  167. error = HV_S_CONT;
  168. break;
  169. case KVP_OP_REGISTER1:
  170. /*
  171. * We use the message header information from
  172. * the user level daemon to transmit errors.
  173. */
  174. error = message->error;
  175. break;
  176. }
  177. /*
  178. * Complete the transaction by forwarding the key value
  179. * to the host. But first, cancel the timeout.
  180. */
  181. if (cancel_delayed_work_sync(&kvp_work))
  182. kvp_respond_to_host(message, error);
  183. }
  184. static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
  185. {
  186. struct hv_kvp_msg *in = in_msg;
  187. struct hv_kvp_ip_msg *out = out_msg;
  188. int len;
  189. switch (op) {
  190. case KVP_OP_GET_IP_INFO:
  191. /*
  192. * Transform all parameters into utf16 encoding.
  193. */
  194. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
  195. strlen((char *)in->body.kvp_ip_val.ip_addr),
  196. UTF16_HOST_ENDIAN,
  197. (wchar_t *)out->kvp_ip_val.ip_addr,
  198. MAX_IP_ADDR_SIZE);
  199. if (len < 0)
  200. return len;
  201. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
  202. strlen((char *)in->body.kvp_ip_val.sub_net),
  203. UTF16_HOST_ENDIAN,
  204. (wchar_t *)out->kvp_ip_val.sub_net,
  205. MAX_IP_ADDR_SIZE);
  206. if (len < 0)
  207. return len;
  208. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
  209. strlen((char *)in->body.kvp_ip_val.gate_way),
  210. UTF16_HOST_ENDIAN,
  211. (wchar_t *)out->kvp_ip_val.gate_way,
  212. MAX_GATEWAY_SIZE);
  213. if (len < 0)
  214. return len;
  215. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
  216. strlen((char *)in->body.kvp_ip_val.dns_addr),
  217. UTF16_HOST_ENDIAN,
  218. (wchar_t *)out->kvp_ip_val.dns_addr,
  219. MAX_IP_ADDR_SIZE);
  220. if (len < 0)
  221. return len;
  222. len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
  223. strlen((char *)in->body.kvp_ip_val.adapter_id),
  224. UTF16_HOST_ENDIAN,
  225. (wchar_t *)out->kvp_ip_val.adapter_id,
  226. MAX_IP_ADDR_SIZE);
  227. if (len < 0)
  228. return len;
  229. out->kvp_ip_val.dhcp_enabled =
  230. in->body.kvp_ip_val.dhcp_enabled;
  231. out->kvp_ip_val.addr_family =
  232. in->body.kvp_ip_val.addr_family;
  233. }
  234. return 0;
  235. }
  236. static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
  237. {
  238. struct hv_kvp_ip_msg *in = in_msg;
  239. struct hv_kvp_msg *out = out_msg;
  240. switch (op) {
  241. case KVP_OP_SET_IP_INFO:
  242. /*
  243. * Transform all parameters into utf8 encoding.
  244. */
  245. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
  246. MAX_IP_ADDR_SIZE,
  247. UTF16_LITTLE_ENDIAN,
  248. (__u8 *)out->body.kvp_ip_val.ip_addr,
  249. MAX_IP_ADDR_SIZE);
  250. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
  251. MAX_IP_ADDR_SIZE,
  252. UTF16_LITTLE_ENDIAN,
  253. (__u8 *)out->body.kvp_ip_val.sub_net,
  254. MAX_IP_ADDR_SIZE);
  255. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
  256. MAX_GATEWAY_SIZE,
  257. UTF16_LITTLE_ENDIAN,
  258. (__u8 *)out->body.kvp_ip_val.gate_way,
  259. MAX_GATEWAY_SIZE);
  260. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
  261. MAX_IP_ADDR_SIZE,
  262. UTF16_LITTLE_ENDIAN,
  263. (__u8 *)out->body.kvp_ip_val.dns_addr,
  264. MAX_IP_ADDR_SIZE);
  265. out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
  266. default:
  267. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
  268. MAX_ADAPTER_ID_SIZE,
  269. UTF16_LITTLE_ENDIAN,
  270. (__u8 *)out->body.kvp_ip_val.adapter_id,
  271. MAX_ADAPTER_ID_SIZE);
  272. out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
  273. }
  274. }
  275. static void
  276. kvp_send_key(struct work_struct *dummy)
  277. {
  278. struct cn_msg *msg;
  279. struct hv_kvp_msg *message;
  280. struct hv_kvp_msg *in_msg;
  281. __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
  282. __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
  283. __u32 val32;
  284. __u64 val64;
  285. msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
  286. if (!msg)
  287. return;
  288. msg->id.idx = CN_KVP_IDX;
  289. msg->id.val = CN_KVP_VAL;
  290. message = (struct hv_kvp_msg *)msg->data;
  291. message->kvp_hdr.operation = operation;
  292. message->kvp_hdr.pool = pool;
  293. in_msg = kvp_transaction.kvp_msg;
  294. /*
  295. * The key/value strings sent from the host are encoded in
  296. * in utf16; convert it to utf8 strings.
  297. * The host assures us that the utf16 strings will not exceed
  298. * the max lengths specified. We will however, reserve room
  299. * for the string terminating character - in the utf16s_utf8s()
  300. * function we limit the size of the buffer where the converted
  301. * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
  302. * that the strings can be properly terminated!
  303. */
  304. switch (message->kvp_hdr.operation) {
  305. case KVP_OP_SET_IP_INFO:
  306. process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
  307. break;
  308. case KVP_OP_GET_IP_INFO:
  309. process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
  310. break;
  311. case KVP_OP_SET:
  312. switch (in_msg->body.kvp_set.data.value_type) {
  313. case REG_SZ:
  314. /*
  315. * The value is a string - utf16 encoding.
  316. */
  317. message->body.kvp_set.data.value_size =
  318. utf16s_to_utf8s(
  319. (wchar_t *)in_msg->body.kvp_set.data.value,
  320. in_msg->body.kvp_set.data.value_size,
  321. UTF16_LITTLE_ENDIAN,
  322. message->body.kvp_set.data.value,
  323. HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
  324. break;
  325. case REG_U32:
  326. /*
  327. * The value is a 32 bit scalar.
  328. * We save this as a utf8 string.
  329. */
  330. val32 = in_msg->body.kvp_set.data.value_u32;
  331. message->body.kvp_set.data.value_size =
  332. sprintf(message->body.kvp_set.data.value,
  333. "%d", val32) + 1;
  334. break;
  335. case REG_U64:
  336. /*
  337. * The value is a 64 bit scalar.
  338. * We save this as a utf8 string.
  339. */
  340. val64 = in_msg->body.kvp_set.data.value_u64;
  341. message->body.kvp_set.data.value_size =
  342. sprintf(message->body.kvp_set.data.value,
  343. "%llu", val64) + 1;
  344. break;
  345. }
  346. case KVP_OP_GET:
  347. message->body.kvp_set.data.key_size =
  348. utf16s_to_utf8s(
  349. (wchar_t *)in_msg->body.kvp_set.data.key,
  350. in_msg->body.kvp_set.data.key_size,
  351. UTF16_LITTLE_ENDIAN,
  352. message->body.kvp_set.data.key,
  353. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  354. break;
  355. case KVP_OP_DELETE:
  356. message->body.kvp_delete.key_size =
  357. utf16s_to_utf8s(
  358. (wchar_t *)in_msg->body.kvp_delete.key,
  359. in_msg->body.kvp_delete.key_size,
  360. UTF16_LITTLE_ENDIAN,
  361. message->body.kvp_delete.key,
  362. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  363. break;
  364. case KVP_OP_ENUMERATE:
  365. message->body.kvp_enum_data.index =
  366. in_msg->body.kvp_enum_data.index;
  367. break;
  368. }
  369. msg->len = sizeof(struct hv_kvp_msg);
  370. cn_netlink_send(msg, 0, GFP_ATOMIC);
  371. kfree(msg);
  372. return;
  373. }
  374. /*
  375. * Send a response back to the host.
  376. */
  377. static void
  378. kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
  379. {
  380. struct hv_kvp_msg *kvp_msg;
  381. struct hv_kvp_exchg_msg_value *kvp_data;
  382. char *key_name;
  383. char *value;
  384. struct icmsg_hdr *icmsghdrp;
  385. int keylen = 0;
  386. int valuelen = 0;
  387. u32 buf_len;
  388. struct vmbus_channel *channel;
  389. u64 req_id;
  390. int ret;
  391. /*
  392. * If a transaction is not active; log and return.
  393. */
  394. if (!kvp_transaction.active) {
  395. /*
  396. * This is a spurious call!
  397. */
  398. pr_warn("KVP: Transaction not active\n");
  399. return;
  400. }
  401. /*
  402. * Copy the global state for completing the transaction. Note that
  403. * only one transaction can be active at a time.
  404. */
  405. buf_len = kvp_transaction.recv_len;
  406. channel = kvp_transaction.recv_channel;
  407. req_id = kvp_transaction.recv_req_id;
  408. kvp_transaction.active = false;
  409. icmsghdrp = (struct icmsg_hdr *)
  410. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  411. if (channel->onchannel_callback == NULL)
  412. /*
  413. * We have raced with util driver being unloaded;
  414. * silently return.
  415. */
  416. return;
  417. icmsghdrp->status = error;
  418. /*
  419. * If the error parameter is set, terminate the host's enumeration
  420. * on this pool.
  421. */
  422. if (error) {
  423. /*
  424. * Something failed or we have timedout;
  425. * terminate the current host-side iteration.
  426. */
  427. goto response_done;
  428. }
  429. kvp_msg = (struct hv_kvp_msg *)
  430. &recv_buffer[sizeof(struct vmbuspipe_hdr) +
  431. sizeof(struct icmsg_hdr)];
  432. switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
  433. case KVP_OP_GET_IP_INFO:
  434. ret = process_ob_ipinfo(msg_to_host,
  435. (struct hv_kvp_ip_msg *)kvp_msg,
  436. KVP_OP_GET_IP_INFO);
  437. if (ret < 0)
  438. icmsghdrp->status = HV_E_FAIL;
  439. goto response_done;
  440. case KVP_OP_SET_IP_INFO:
  441. goto response_done;
  442. case KVP_OP_GET:
  443. kvp_data = &kvp_msg->body.kvp_get.data;
  444. goto copy_value;
  445. case KVP_OP_SET:
  446. case KVP_OP_DELETE:
  447. goto response_done;
  448. default:
  449. break;
  450. }
  451. kvp_data = &kvp_msg->body.kvp_enum_data.data;
  452. key_name = msg_to_host->body.kvp_enum_data.data.key;
  453. /*
  454. * The windows host expects the key/value pair to be encoded
  455. * in utf16. Ensure that the key/value size reported to the host
  456. * will be less than or equal to the MAX size (including the
  457. * terminating character).
  458. */
  459. keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
  460. (wchar_t *) kvp_data->key,
  461. (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
  462. kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
  463. copy_value:
  464. value = msg_to_host->body.kvp_enum_data.data.value;
  465. valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
  466. (wchar_t *) kvp_data->value,
  467. (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
  468. kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
  469. /*
  470. * If the utf8s to utf16s conversion failed; notify host
  471. * of the error.
  472. */
  473. if ((keylen < 0) || (valuelen < 0))
  474. icmsghdrp->status = HV_E_FAIL;
  475. kvp_data->value_type = REG_SZ; /* all our values are strings */
  476. response_done:
  477. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  478. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  479. VM_PKT_DATA_INBAND, 0);
  480. }
  481. /*
  482. * This callback is invoked when we get a KVP message from the host.
  483. * The host ensures that only one KVP transaction can be active at a time.
  484. * KVP implementation in Linux needs to forward the key to a user-mde
  485. * component to retrive the corresponding value. Consequently, we cannot
  486. * respond to the host in the conext of this callback. Since the host
  487. * guarantees that at most only one transaction can be active at a time,
  488. * we stash away the transaction state in a set of global variables.
  489. */
  490. void hv_kvp_onchannelcallback(void *context)
  491. {
  492. struct vmbus_channel *channel = context;
  493. u32 recvlen;
  494. u64 requestid;
  495. struct hv_kvp_msg *kvp_msg;
  496. struct icmsg_hdr *icmsghdrp;
  497. struct icmsg_negotiate *negop = NULL;
  498. if (kvp_transaction.active) {
  499. /*
  500. * We will defer processing this callback once
  501. * the current transaction is complete.
  502. */
  503. kvp_transaction.kvp_context = context;
  504. return;
  505. }
  506. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
  507. &requestid);
  508. if (recvlen > 0) {
  509. icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
  510. sizeof(struct vmbuspipe_hdr)];
  511. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  512. /*
  513. * We start with win8 version and if the host cannot
  514. * support that we use the previous version.
  515. */
  516. if (vmbus_prep_negotiate_resp(icmsghdrp, negop,
  517. recv_buffer, UTIL_FW_MAJOR_MINOR,
  518. WIN8_SRV_MAJOR_MINOR))
  519. goto done;
  520. vmbus_prep_negotiate_resp(icmsghdrp, negop,
  521. recv_buffer, UTIL_FW_MAJOR_MINOR,
  522. WIN7_SRV_MAJOR_MINOR);
  523. } else {
  524. kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
  525. sizeof(struct vmbuspipe_hdr) +
  526. sizeof(struct icmsg_hdr)];
  527. /*
  528. * Stash away this global state for completing the
  529. * transaction; note transactions are serialized.
  530. */
  531. kvp_transaction.recv_len = recvlen;
  532. kvp_transaction.recv_channel = channel;
  533. kvp_transaction.recv_req_id = requestid;
  534. kvp_transaction.active = true;
  535. kvp_transaction.kvp_msg = kvp_msg;
  536. /*
  537. * Get the information from the
  538. * user-mode component.
  539. * component. This transaction will be
  540. * completed when we get the value from
  541. * the user-mode component.
  542. * Set a timeout to deal with
  543. * user-mode not responding.
  544. */
  545. schedule_work(&kvp_sendkey_work);
  546. schedule_delayed_work(&kvp_work, 5*HZ);
  547. return;
  548. }
  549. done:
  550. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  551. | ICMSGHDRFLAG_RESPONSE;
  552. vmbus_sendpacket(channel, recv_buffer,
  553. recvlen, requestid,
  554. VM_PKT_DATA_INBAND, 0);
  555. }
  556. }
  557. int
  558. hv_kvp_init(struct hv_util_service *srv)
  559. {
  560. int err;
  561. err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
  562. if (err)
  563. return err;
  564. recv_buffer = srv->recv_buffer;
  565. /*
  566. * When this driver loads, the user level daemon that
  567. * processes the host requests may not yet be running.
  568. * Defer processing channel callbacks until the daemon
  569. * has registered.
  570. */
  571. kvp_transaction.active = true;
  572. return 0;
  573. }
  574. void hv_kvp_deinit(void)
  575. {
  576. cn_del_callback(&kvp_id);
  577. cancel_delayed_work_sync(&kvp_work);
  578. cancel_work_sync(&kvp_sendkey_work);
  579. }