hv_kvp.c 20 KB

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