hv_kvp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "hv_kvp.h"
  30. /*
  31. * Global state maintained for transaction that is being processed.
  32. * Note that only one transaction can be active at any point in time.
  33. *
  34. * This state is set when we receive a request from the host; we
  35. * cleanup this state when the transaction is completed - when we respond
  36. * to the host with the key value.
  37. */
  38. static struct {
  39. bool active; /* transaction status - active or not */
  40. int recv_len; /* number of bytes received. */
  41. int index; /* current index */
  42. struct vmbus_channel *recv_channel; /* chn we got the request */
  43. u64 recv_req_id; /* request ID. */
  44. } kvp_transaction;
  45. static void kvp_send_key(struct work_struct *dummy);
  46. #define TIMEOUT_FIRED 1
  47. static void kvp_respond_to_host(char *key, char *value, int error);
  48. static void kvp_work_func(struct work_struct *dummy);
  49. static void kvp_register(void);
  50. static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
  51. static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
  52. static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL };
  53. static const char kvp_name[] = "kvp_kernel_module";
  54. static u8 *recv_buffer;
  55. /*
  56. * Register the kernel component with the user-level daemon.
  57. * As part of this registration, pass the LIC version number.
  58. */
  59. static void
  60. kvp_register(void)
  61. {
  62. struct cn_msg *msg;
  63. msg = kzalloc(sizeof(*msg) + strlen(HV_DRV_VERSION) + 1 , GFP_ATOMIC);
  64. if (msg) {
  65. msg->id.idx = CN_KVP_IDX;
  66. msg->id.val = CN_KVP_VAL;
  67. msg->seq = KVP_REGISTER;
  68. strcpy(msg->data, HV_DRV_VERSION);
  69. msg->len = strlen(HV_DRV_VERSION) + 1;
  70. cn_netlink_send(msg, 0, GFP_ATOMIC);
  71. kfree(msg);
  72. }
  73. }
  74. static void
  75. kvp_work_func(struct work_struct *dummy)
  76. {
  77. /*
  78. * If the timer fires, the user-mode component has not responded;
  79. * process the pending transaction.
  80. */
  81. kvp_respond_to_host("Unknown key", "Guest timed out", TIMEOUT_FIRED);
  82. }
  83. /*
  84. * Callback when data is received from user mode.
  85. */
  86. static void
  87. kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
  88. {
  89. struct hv_ku_msg *message;
  90. message = (struct hv_ku_msg *)msg->data;
  91. if (msg->seq == KVP_REGISTER) {
  92. pr_info("KVP: user-mode registering done.\n");
  93. kvp_register();
  94. }
  95. if (msg->seq == KVP_USER_SET) {
  96. /*
  97. * Complete the transaction by forwarding the key value
  98. * to the host. But first, cancel the timeout.
  99. */
  100. if (cancel_delayed_work_sync(&kvp_work))
  101. kvp_respond_to_host(message->kvp_key,
  102. message->kvp_value,
  103. !strlen(message->kvp_key));
  104. }
  105. }
  106. static void
  107. kvp_send_key(struct work_struct *dummy)
  108. {
  109. struct cn_msg *msg;
  110. int index = kvp_transaction.index;
  111. msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
  112. if (msg) {
  113. msg->id.idx = CN_KVP_IDX;
  114. msg->id.val = CN_KVP_VAL;
  115. msg->seq = KVP_KERNEL_GET;
  116. ((struct hv_ku_msg *)msg->data)->kvp_index = index;
  117. msg->len = sizeof(struct hv_ku_msg);
  118. cn_netlink_send(msg, 0, GFP_ATOMIC);
  119. kfree(msg);
  120. }
  121. return;
  122. }
  123. /*
  124. * Send a response back to the host.
  125. */
  126. static void
  127. kvp_respond_to_host(char *key, char *value, int error)
  128. {
  129. struct hv_kvp_msg *kvp_msg;
  130. struct hv_kvp_msg_enumerate *kvp_data;
  131. char *key_name;
  132. struct icmsg_hdr *icmsghdrp;
  133. int keylen, valuelen;
  134. u32 buf_len;
  135. struct vmbus_channel *channel;
  136. u64 req_id;
  137. /*
  138. * If a transaction is not active; log and return.
  139. */
  140. if (!kvp_transaction.active) {
  141. /*
  142. * This is a spurious call!
  143. */
  144. pr_warn("KVP: Transaction not active\n");
  145. return;
  146. }
  147. /*
  148. * Copy the global state for completing the transaction. Note that
  149. * only one transaction can be active at a time.
  150. */
  151. buf_len = kvp_transaction.recv_len;
  152. channel = kvp_transaction.recv_channel;
  153. req_id = kvp_transaction.recv_req_id;
  154. kvp_transaction.active = false;
  155. if (channel->onchannel_callback == NULL)
  156. /*
  157. * We have raced with util driver being unloaded;
  158. * silently return.
  159. */
  160. return;
  161. icmsghdrp = (struct icmsg_hdr *)
  162. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  163. kvp_msg = (struct hv_kvp_msg *)
  164. &recv_buffer[sizeof(struct vmbuspipe_hdr) +
  165. sizeof(struct icmsg_hdr)];
  166. kvp_data = &kvp_msg->kvp_data;
  167. key_name = key;
  168. /*
  169. * If the error parameter is set, terminate the host's enumeration.
  170. */
  171. if (error) {
  172. /*
  173. * We don't support this index or the we have timedout;
  174. * terminate the host-side iteration by returning an error.
  175. */
  176. icmsghdrp->status = HV_E_FAIL;
  177. goto response_done;
  178. }
  179. /*
  180. * The windows host expects the key/value pair to be encoded
  181. * in utf16.
  182. */
  183. keylen = utf8s_to_utf16s(key_name, strlen(key_name),
  184. (wchar_t *)kvp_data->data.key);
  185. kvp_data->data.key_size = 2*(keylen + 1); /* utf16 encoding */
  186. valuelen = utf8s_to_utf16s(value, strlen(value),
  187. (wchar_t *)kvp_data->data.value);
  188. kvp_data->data.value_size = 2*(valuelen + 1); /* utf16 encoding */
  189. kvp_data->data.value_type = REG_SZ; /* all our values are strings */
  190. icmsghdrp->status = HV_S_OK;
  191. response_done:
  192. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  193. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  194. VM_PKT_DATA_INBAND, 0);
  195. }
  196. /*
  197. * This callback is invoked when we get a KVP message from the host.
  198. * The host ensures that only one KVP transaction can be active at a time.
  199. * KVP implementation in Linux needs to forward the key to a user-mde
  200. * component to retrive the corresponding value. Consequently, we cannot
  201. * respond to the host in the conext of this callback. Since the host
  202. * guarantees that at most only one transaction can be active at a time,
  203. * we stash away the transaction state in a set of global variables.
  204. */
  205. void hv_kvp_onchannelcallback(void *context)
  206. {
  207. struct vmbus_channel *channel = context;
  208. u32 recvlen;
  209. u64 requestid;
  210. struct hv_kvp_msg *kvp_msg;
  211. struct hv_kvp_msg_enumerate *kvp_data;
  212. struct icmsg_hdr *icmsghdrp;
  213. struct icmsg_negotiate *negop = NULL;
  214. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
  215. if (recvlen > 0) {
  216. icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
  217. sizeof(struct vmbuspipe_hdr)];
  218. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  219. vmbus_prep_negotiate_resp(icmsghdrp, negop, recv_buffer);
  220. } else {
  221. kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
  222. sizeof(struct vmbuspipe_hdr) +
  223. sizeof(struct icmsg_hdr)];
  224. kvp_data = &kvp_msg->kvp_data;
  225. /*
  226. * We only support the "get" operation on
  227. * "KVP_POOL_AUTO" pool.
  228. */
  229. if ((kvp_msg->kvp_hdr.pool != KVP_POOL_AUTO) ||
  230. (kvp_msg->kvp_hdr.operation !=
  231. KVP_OP_ENUMERATE)) {
  232. icmsghdrp->status = HV_E_FAIL;
  233. goto callback_done;
  234. }
  235. /*
  236. * Stash away this global state for completing the
  237. * transaction; note transactions are serialized.
  238. */
  239. kvp_transaction.recv_len = recvlen;
  240. kvp_transaction.recv_channel = channel;
  241. kvp_transaction.recv_req_id = requestid;
  242. kvp_transaction.active = true;
  243. kvp_transaction.index = kvp_data->index;
  244. /*
  245. * Get the information from the
  246. * user-mode component.
  247. * component. This transaction will be
  248. * completed when we get the value from
  249. * the user-mode component.
  250. * Set a timeout to deal with
  251. * user-mode not responding.
  252. */
  253. schedule_work(&kvp_sendkey_work);
  254. schedule_delayed_work(&kvp_work, 5*HZ);
  255. return;
  256. }
  257. callback_done:
  258. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  259. | ICMSGHDRFLAG_RESPONSE;
  260. vmbus_sendpacket(channel, recv_buffer,
  261. recvlen, requestid,
  262. VM_PKT_DATA_INBAND, 0);
  263. }
  264. }
  265. int
  266. hv_kvp_init(struct hv_util_service *srv)
  267. {
  268. int err;
  269. err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
  270. if (err)
  271. return err;
  272. recv_buffer = srv->recv_buffer;
  273. return 0;
  274. }
  275. void hv_kvp_deinit(void)
  276. {
  277. cn_del_callback(&kvp_id);
  278. cancel_delayed_work_sync(&kvp_work);
  279. cancel_work_sync(&kvp_sendkey_work);
  280. }