hv_kvp.c 20 KB

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