hv_kvp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. /* fallthrough */
  299. case KVP_OP_GET_IP_INFO:
  300. utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
  301. MAX_ADAPTER_ID_SIZE,
  302. UTF16_LITTLE_ENDIAN,
  303. (__u8 *)out->body.kvp_ip_val.adapter_id,
  304. MAX_ADAPTER_ID_SIZE);
  305. out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
  306. }
  307. }
  308. static void
  309. kvp_send_key(struct work_struct *dummy)
  310. {
  311. struct hv_kvp_msg *message;
  312. struct hv_kvp_msg *in_msg;
  313. __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
  314. __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
  315. __u32 val32;
  316. __u64 val64;
  317. int rc;
  318. /* The transaction state is wrong. */
  319. if (kvp_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
  320. return;
  321. message = kzalloc(sizeof(*message), GFP_KERNEL);
  322. if (!message)
  323. return;
  324. message->kvp_hdr.operation = operation;
  325. message->kvp_hdr.pool = pool;
  326. in_msg = kvp_transaction.kvp_msg;
  327. /*
  328. * The key/value strings sent from the host are encoded in
  329. * in utf16; convert it to utf8 strings.
  330. * The host assures us that the utf16 strings will not exceed
  331. * the max lengths specified. We will however, reserve room
  332. * for the string terminating character - in the utf16s_utf8s()
  333. * function we limit the size of the buffer where the converted
  334. * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to guarantee
  335. * that the strings can be properly terminated!
  336. */
  337. switch (message->kvp_hdr.operation) {
  338. case KVP_OP_SET_IP_INFO:
  339. process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
  340. break;
  341. case KVP_OP_GET_IP_INFO:
  342. /*
  343. * We only need to pass on the info of operation, adapter_id
  344. * and addr_family to the userland kvp daemon.
  345. */
  346. process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
  347. break;
  348. case KVP_OP_SET:
  349. switch (in_msg->body.kvp_set.data.value_type) {
  350. case REG_SZ:
  351. /*
  352. * The value is a string - utf16 encoding.
  353. */
  354. message->body.kvp_set.data.value_size =
  355. utf16s_to_utf8s(
  356. (wchar_t *)in_msg->body.kvp_set.data.value,
  357. in_msg->body.kvp_set.data.value_size,
  358. UTF16_LITTLE_ENDIAN,
  359. message->body.kvp_set.data.value,
  360. HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
  361. break;
  362. case REG_U32:
  363. /*
  364. * The value is a 32 bit scalar.
  365. * We save this as a utf8 string.
  366. */
  367. val32 = in_msg->body.kvp_set.data.value_u32;
  368. message->body.kvp_set.data.value_size =
  369. sprintf(message->body.kvp_set.data.value,
  370. "%d", val32) + 1;
  371. break;
  372. case REG_U64:
  373. /*
  374. * The value is a 64 bit scalar.
  375. * We save this as a utf8 string.
  376. */
  377. val64 = in_msg->body.kvp_set.data.value_u64;
  378. message->body.kvp_set.data.value_size =
  379. sprintf(message->body.kvp_set.data.value,
  380. "%llu", val64) + 1;
  381. break;
  382. }
  383. /*
  384. * The key is always a string - utf16 encoding.
  385. */
  386. message->body.kvp_set.data.key_size =
  387. utf16s_to_utf8s(
  388. (wchar_t *)in_msg->body.kvp_set.data.key,
  389. in_msg->body.kvp_set.data.key_size,
  390. UTF16_LITTLE_ENDIAN,
  391. message->body.kvp_set.data.key,
  392. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  393. break;
  394. case KVP_OP_GET:
  395. message->body.kvp_get.data.key_size =
  396. utf16s_to_utf8s(
  397. (wchar_t *)in_msg->body.kvp_get.data.key,
  398. in_msg->body.kvp_get.data.key_size,
  399. UTF16_LITTLE_ENDIAN,
  400. message->body.kvp_get.data.key,
  401. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  402. break;
  403. case KVP_OP_DELETE:
  404. message->body.kvp_delete.key_size =
  405. utf16s_to_utf8s(
  406. (wchar_t *)in_msg->body.kvp_delete.key,
  407. in_msg->body.kvp_delete.key_size,
  408. UTF16_LITTLE_ENDIAN,
  409. message->body.kvp_delete.key,
  410. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  411. break;
  412. case KVP_OP_ENUMERATE:
  413. message->body.kvp_enum_data.index =
  414. in_msg->body.kvp_enum_data.index;
  415. break;
  416. }
  417. kvp_transaction.state = HVUTIL_USERSPACE_REQ;
  418. rc = hvutil_transport_send(hvt, message, sizeof(*message), NULL);
  419. if (rc) {
  420. pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
  421. if (cancel_delayed_work_sync(&kvp_timeout_work)) {
  422. kvp_respond_to_host(message, HV_E_FAIL);
  423. kvp_transaction.state = HVUTIL_READY;
  424. }
  425. }
  426. kfree(message);
  427. }
  428. /*
  429. * Send a response back to the host.
  430. */
  431. static void
  432. kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
  433. {
  434. struct hv_kvp_msg *kvp_msg;
  435. struct hv_kvp_exchg_msg_value *kvp_data;
  436. char *key_name;
  437. char *value;
  438. struct icmsg_hdr *icmsghdrp;
  439. int keylen = 0;
  440. int valuelen = 0;
  441. u32 buf_len;
  442. struct vmbus_channel *channel;
  443. u64 req_id;
  444. int ret;
  445. /*
  446. * Copy the global state for completing the transaction. Note that
  447. * only one transaction can be active at a time.
  448. */
  449. buf_len = kvp_transaction.recv_len;
  450. channel = kvp_transaction.recv_channel;
  451. req_id = kvp_transaction.recv_req_id;
  452. icmsghdrp = (struct icmsg_hdr *)
  453. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  454. if (channel->onchannel_callback == NULL)
  455. /*
  456. * We have raced with util driver being unloaded;
  457. * silently return.
  458. */
  459. return;
  460. icmsghdrp->status = error;
  461. /*
  462. * If the error parameter is set, terminate the host's enumeration
  463. * on this pool.
  464. */
  465. if (error) {
  466. /*
  467. * Something failed or we have timed out;
  468. * terminate the current host-side iteration.
  469. */
  470. goto response_done;
  471. }
  472. kvp_msg = (struct hv_kvp_msg *)
  473. &recv_buffer[sizeof(struct vmbuspipe_hdr) +
  474. sizeof(struct icmsg_hdr)];
  475. switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
  476. case KVP_OP_GET_IP_INFO:
  477. ret = process_ob_ipinfo(msg_to_host,
  478. (struct hv_kvp_ip_msg *)kvp_msg,
  479. KVP_OP_GET_IP_INFO);
  480. if (ret < 0)
  481. icmsghdrp->status = HV_E_FAIL;
  482. goto response_done;
  483. case KVP_OP_SET_IP_INFO:
  484. goto response_done;
  485. case KVP_OP_GET:
  486. kvp_data = &kvp_msg->body.kvp_get.data;
  487. goto copy_value;
  488. case KVP_OP_SET:
  489. case KVP_OP_DELETE:
  490. goto response_done;
  491. default:
  492. break;
  493. }
  494. kvp_data = &kvp_msg->body.kvp_enum_data.data;
  495. key_name = msg_to_host->body.kvp_enum_data.data.key;
  496. /*
  497. * The windows host expects the key/value pair to be encoded
  498. * in utf16. Ensure that the key/value size reported to the host
  499. * will be less than or equal to the MAX size (including the
  500. * terminating character).
  501. */
  502. keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
  503. (wchar_t *) kvp_data->key,
  504. (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
  505. kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
  506. copy_value:
  507. value = msg_to_host->body.kvp_enum_data.data.value;
  508. valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
  509. (wchar_t *) kvp_data->value,
  510. (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
  511. kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
  512. /*
  513. * If the utf8s to utf16s conversion failed; notify host
  514. * of the error.
  515. */
  516. if ((keylen < 0) || (valuelen < 0))
  517. icmsghdrp->status = HV_E_FAIL;
  518. kvp_data->value_type = REG_SZ; /* all our values are strings */
  519. response_done:
  520. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  521. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  522. VM_PKT_DATA_INBAND, 0);
  523. }
  524. /*
  525. * This callback is invoked when we get a KVP message from the host.
  526. * The host ensures that only one KVP transaction can be active at a time.
  527. * KVP implementation in Linux needs to forward the key to a user-mde
  528. * component to retrieve the corresponding value. Consequently, we cannot
  529. * respond to the host in the context of this callback. Since the host
  530. * guarantees that at most only one transaction can be active at a time,
  531. * we stash away the transaction state in a set of global variables.
  532. */
  533. void hv_kvp_onchannelcallback(void *context)
  534. {
  535. struct vmbus_channel *channel = context;
  536. u32 recvlen;
  537. u64 requestid;
  538. struct hv_kvp_msg *kvp_msg;
  539. struct icmsg_hdr *icmsghdrp;
  540. int kvp_srv_version;
  541. static enum {NEGO_NOT_STARTED,
  542. NEGO_IN_PROGRESS,
  543. NEGO_FINISHED} host_negotiatied = NEGO_NOT_STARTED;
  544. if (kvp_transaction.state < HVUTIL_READY) {
  545. /*
  546. * If userspace daemon is not connected and host is asking
  547. * us to negotiate we need to delay to not lose messages.
  548. * This is important for Failover IP setting.
  549. */
  550. if (host_negotiatied == NEGO_NOT_STARTED) {
  551. host_negotiatied = NEGO_IN_PROGRESS;
  552. schedule_delayed_work(&kvp_host_handshake_work,
  553. HV_UTIL_NEGO_TIMEOUT * HZ);
  554. }
  555. return;
  556. }
  557. if (kvp_transaction.state > HVUTIL_READY)
  558. return;
  559. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
  560. &requestid);
  561. if (recvlen > 0) {
  562. icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
  563. sizeof(struct vmbuspipe_hdr)];
  564. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  565. if (vmbus_prep_negotiate_resp(icmsghdrp,
  566. recv_buffer, fw_versions, FW_VER_COUNT,
  567. kvp_versions, KVP_VER_COUNT,
  568. NULL, &kvp_srv_version)) {
  569. pr_info("KVP IC version %d.%d\n",
  570. kvp_srv_version >> 16,
  571. kvp_srv_version & 0xFFFF);
  572. }
  573. } else {
  574. kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
  575. sizeof(struct vmbuspipe_hdr) +
  576. sizeof(struct icmsg_hdr)];
  577. /*
  578. * Stash away this global state for completing the
  579. * transaction; note transactions are serialized.
  580. */
  581. kvp_transaction.recv_len = recvlen;
  582. kvp_transaction.recv_req_id = requestid;
  583. kvp_transaction.kvp_msg = kvp_msg;
  584. if (kvp_transaction.state < HVUTIL_READY) {
  585. /* Userspace is not registered yet */
  586. kvp_respond_to_host(NULL, HV_E_FAIL);
  587. return;
  588. }
  589. kvp_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
  590. /*
  591. * Get the information from the
  592. * user-mode component.
  593. * component. This transaction will be
  594. * completed when we get the value from
  595. * the user-mode component.
  596. * Set a timeout to deal with
  597. * user-mode not responding.
  598. */
  599. schedule_work(&kvp_sendkey_work);
  600. schedule_delayed_work(&kvp_timeout_work,
  601. HV_UTIL_TIMEOUT * HZ);
  602. return;
  603. }
  604. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  605. | ICMSGHDRFLAG_RESPONSE;
  606. vmbus_sendpacket(channel, recv_buffer,
  607. recvlen, requestid,
  608. VM_PKT_DATA_INBAND, 0);
  609. host_negotiatied = NEGO_FINISHED;
  610. hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
  611. }
  612. }
  613. static void kvp_on_reset(void)
  614. {
  615. if (cancel_delayed_work_sync(&kvp_timeout_work))
  616. kvp_respond_to_host(NULL, HV_E_FAIL);
  617. kvp_transaction.state = HVUTIL_DEVICE_INIT;
  618. }
  619. int
  620. hv_kvp_init(struct hv_util_service *srv)
  621. {
  622. recv_buffer = srv->recv_buffer;
  623. kvp_transaction.recv_channel = srv->channel;
  624. /*
  625. * When this driver loads, the user level daemon that
  626. * processes the host requests may not yet be running.
  627. * Defer processing channel callbacks until the daemon
  628. * has registered.
  629. */
  630. kvp_transaction.state = HVUTIL_DEVICE_INIT;
  631. hvt = hvutil_transport_init(kvp_devname, CN_KVP_IDX, CN_KVP_VAL,
  632. kvp_on_msg, kvp_on_reset);
  633. if (!hvt)
  634. return -EFAULT;
  635. return 0;
  636. }
  637. void hv_kvp_deinit(void)
  638. {
  639. kvp_transaction.state = HVUTIL_DEVICE_DYING;
  640. cancel_delayed_work_sync(&kvp_host_handshake_work);
  641. cancel_delayed_work_sync(&kvp_timeout_work);
  642. cancel_work_sync(&kvp_sendkey_work);
  643. hvutil_transport_destroy(hvt);
  644. }