hv_kvp.c 20 KB

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