hv_kvp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /*
  30. * Global state maintained for transaction that is being processed.
  31. * Note that only one transaction can be active at any point in time.
  32. *
  33. * This state is set when we receive a request from the host; we
  34. * cleanup this state when the transaction is completed - when we respond
  35. * to the host with the key value.
  36. */
  37. static struct {
  38. bool active; /* transaction status - active or not */
  39. int recv_len; /* number of bytes received. */
  40. struct hv_kvp_msg *kvp_msg; /* current message */
  41. struct vmbus_channel *recv_channel; /* chn we got the request */
  42. u64 recv_req_id; /* request ID. */
  43. void *kvp_context; /* for the channel callback */
  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. struct hv_kvp_msg *kvp_msg;
  64. char *version;
  65. msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC);
  66. if (msg) {
  67. kvp_msg = (struct hv_kvp_msg *)msg->data;
  68. version = kvp_msg->body.kvp_register.version;
  69. msg->id.idx = CN_KVP_IDX;
  70. msg->id.val = CN_KVP_VAL;
  71. kvp_msg->kvp_hdr.operation = KVP_OP_REGISTER;
  72. strcpy(version, HV_DRV_VERSION);
  73. msg->len = sizeof(struct hv_kvp_msg);
  74. cn_netlink_send(msg, 0, GFP_ATOMIC);
  75. kfree(msg);
  76. }
  77. }
  78. static void
  79. kvp_work_func(struct work_struct *dummy)
  80. {
  81. /*
  82. * If the timer fires, the user-mode component has not responded;
  83. * process the pending transaction.
  84. */
  85. kvp_respond_to_host("Unknown key", "Guest timed out", TIMEOUT_FIRED);
  86. }
  87. /*
  88. * Callback when data is received from user mode.
  89. */
  90. static void
  91. kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
  92. {
  93. struct hv_kvp_msg *message;
  94. struct hv_kvp_msg_enumerate *data;
  95. message = (struct hv_kvp_msg *)msg->data;
  96. switch (message->kvp_hdr.operation) {
  97. case KVP_OP_REGISTER:
  98. pr_info("KVP: user-mode registering done.\n");
  99. kvp_register();
  100. kvp_transaction.active = false;
  101. hv_kvp_onchannelcallback(kvp_transaction.kvp_context);
  102. break;
  103. default:
  104. data = &message->body.kvp_enum_data;
  105. /*
  106. * Complete the transaction by forwarding the key value
  107. * to the host. But first, cancel the timeout.
  108. */
  109. if (cancel_delayed_work_sync(&kvp_work))
  110. kvp_respond_to_host(data->data.key,
  111. data->data.value,
  112. !strlen(data->data.key));
  113. }
  114. }
  115. static void
  116. kvp_send_key(struct work_struct *dummy)
  117. {
  118. struct cn_msg *msg;
  119. struct hv_kvp_msg *message;
  120. struct hv_kvp_msg *in_msg;
  121. __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
  122. __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
  123. __u32 val32;
  124. __u64 val64;
  125. msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC);
  126. if (!msg)
  127. return;
  128. msg->id.idx = CN_KVP_IDX;
  129. msg->id.val = CN_KVP_VAL;
  130. message = (struct hv_kvp_msg *)msg->data;
  131. message->kvp_hdr.operation = operation;
  132. message->kvp_hdr.pool = pool;
  133. in_msg = kvp_transaction.kvp_msg;
  134. /*
  135. * The key/value strings sent from the host are encoded in
  136. * in utf16; convert it to utf8 strings.
  137. * The host assures us that the utf16 strings will not exceed
  138. * the max lengths specified. We will however, reserve room
  139. * for the string terminating character - in the utf16s_utf8s()
  140. * function we limit the size of the buffer where the converted
  141. * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee
  142. * that the strings can be properly terminated!
  143. */
  144. switch (message->kvp_hdr.operation) {
  145. case KVP_OP_SET:
  146. switch (in_msg->body.kvp_set.data.value_type) {
  147. case REG_SZ:
  148. /*
  149. * The value is a string - utf16 encoding.
  150. */
  151. message->body.kvp_set.data.value_size =
  152. utf16s_to_utf8s(
  153. (wchar_t *)in_msg->body.kvp_set.data.value,
  154. in_msg->body.kvp_set.data.value_size,
  155. UTF16_LITTLE_ENDIAN,
  156. message->body.kvp_set.data.value,
  157. HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
  158. break;
  159. case REG_U32:
  160. /*
  161. * The value is a 32 bit scalar.
  162. * We save this as a utf8 string.
  163. */
  164. val32 = in_msg->body.kvp_set.data.value_u32;
  165. message->body.kvp_set.data.value_size =
  166. sprintf(message->body.kvp_set.data.value,
  167. "%d", val32) + 1;
  168. break;
  169. case REG_U64:
  170. /*
  171. * The value is a 64 bit scalar.
  172. * We save this as a utf8 string.
  173. */
  174. val64 = in_msg->body.kvp_set.data.value_u64;
  175. message->body.kvp_set.data.value_size =
  176. sprintf(message->body.kvp_set.data.value,
  177. "%llu", val64) + 1;
  178. break;
  179. }
  180. case KVP_OP_GET:
  181. message->body.kvp_set.data.key_size =
  182. utf16s_to_utf8s(
  183. (wchar_t *)in_msg->body.kvp_set.data.key,
  184. in_msg->body.kvp_set.data.key_size,
  185. UTF16_LITTLE_ENDIAN,
  186. message->body.kvp_set.data.key,
  187. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  188. break;
  189. case KVP_OP_DELETE:
  190. message->body.kvp_delete.key_size =
  191. utf16s_to_utf8s(
  192. (wchar_t *)in_msg->body.kvp_delete.key,
  193. in_msg->body.kvp_delete.key_size,
  194. UTF16_LITTLE_ENDIAN,
  195. message->body.kvp_delete.key,
  196. HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
  197. break;
  198. case KVP_OP_ENUMERATE:
  199. message->body.kvp_enum_data.index =
  200. in_msg->body.kvp_enum_data.index;
  201. break;
  202. }
  203. msg->len = sizeof(struct hv_kvp_msg);
  204. cn_netlink_send(msg, 0, GFP_ATOMIC);
  205. kfree(msg);
  206. return;
  207. }
  208. /*
  209. * Send a response back to the host.
  210. */
  211. static void
  212. kvp_respond_to_host(char *key, char *value, int error)
  213. {
  214. struct hv_kvp_msg *kvp_msg;
  215. struct hv_kvp_exchg_msg_value *kvp_data;
  216. char *key_name;
  217. struct icmsg_hdr *icmsghdrp;
  218. int keylen = 0;
  219. int valuelen = 0;
  220. u32 buf_len;
  221. struct vmbus_channel *channel;
  222. u64 req_id;
  223. /*
  224. * If a transaction is not active; log and return.
  225. */
  226. if (!kvp_transaction.active) {
  227. /*
  228. * This is a spurious call!
  229. */
  230. pr_warn("KVP: Transaction not active\n");
  231. return;
  232. }
  233. /*
  234. * Copy the global state for completing the transaction. Note that
  235. * only one transaction can be active at a time.
  236. */
  237. buf_len = kvp_transaction.recv_len;
  238. channel = kvp_transaction.recv_channel;
  239. req_id = kvp_transaction.recv_req_id;
  240. kvp_transaction.active = false;
  241. icmsghdrp = (struct icmsg_hdr *)
  242. &recv_buffer[sizeof(struct vmbuspipe_hdr)];
  243. if (channel->onchannel_callback == NULL)
  244. /*
  245. * We have raced with util driver being unloaded;
  246. * silently return.
  247. */
  248. return;
  249. /*
  250. * If the error parameter is set, terminate the host's enumeration
  251. * on this pool.
  252. */
  253. if (error) {
  254. /*
  255. * Something failed or the we have timedout;
  256. * terminate the current host-side iteration.
  257. */
  258. icmsghdrp->status = HV_S_CONT;
  259. goto response_done;
  260. }
  261. icmsghdrp->status = HV_S_OK;
  262. kvp_msg = (struct hv_kvp_msg *)
  263. &recv_buffer[sizeof(struct vmbuspipe_hdr) +
  264. sizeof(struct icmsg_hdr)];
  265. switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
  266. case KVP_OP_GET:
  267. kvp_data = &kvp_msg->body.kvp_get.data;
  268. goto copy_value;
  269. case KVP_OP_SET:
  270. case KVP_OP_DELETE:
  271. goto response_done;
  272. default:
  273. break;
  274. }
  275. kvp_data = &kvp_msg->body.kvp_enum_data.data;
  276. key_name = key;
  277. /*
  278. * The windows host expects the key/value pair to be encoded
  279. * in utf16. Ensure that the key/value size reported to the host
  280. * will be less than or equal to the MAX size (including the
  281. * terminating character).
  282. */
  283. keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
  284. (wchar_t *) kvp_data->key,
  285. (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
  286. kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
  287. copy_value:
  288. valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
  289. (wchar_t *) kvp_data->value,
  290. (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
  291. kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */
  292. /*
  293. * If the utf8s to utf16s conversion failed; notify host
  294. * of the error.
  295. */
  296. if ((keylen < 0) || (valuelen < 0))
  297. icmsghdrp->status = HV_E_FAIL;
  298. kvp_data->value_type = REG_SZ; /* all our values are strings */
  299. response_done:
  300. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  301. vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
  302. VM_PKT_DATA_INBAND, 0);
  303. }
  304. /*
  305. * This callback is invoked when we get a KVP message from the host.
  306. * The host ensures that only one KVP transaction can be active at a time.
  307. * KVP implementation in Linux needs to forward the key to a user-mde
  308. * component to retrive the corresponding value. Consequently, we cannot
  309. * respond to the host in the conext of this callback. Since the host
  310. * guarantees that at most only one transaction can be active at a time,
  311. * we stash away the transaction state in a set of global variables.
  312. */
  313. void hv_kvp_onchannelcallback(void *context)
  314. {
  315. struct vmbus_channel *channel = context;
  316. u32 recvlen;
  317. u64 requestid;
  318. struct hv_kvp_msg *kvp_msg;
  319. struct icmsg_hdr *icmsghdrp;
  320. struct icmsg_negotiate *negop = NULL;
  321. if (kvp_transaction.active) {
  322. /*
  323. * We will defer processing this callback once
  324. * the current transaction is complete.
  325. */
  326. kvp_transaction.kvp_context = context;
  327. return;
  328. }
  329. vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
  330. if (recvlen > 0) {
  331. icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
  332. sizeof(struct vmbuspipe_hdr)];
  333. if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  334. vmbus_prep_negotiate_resp(icmsghdrp, negop, recv_buffer);
  335. } else {
  336. kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
  337. sizeof(struct vmbuspipe_hdr) +
  338. sizeof(struct icmsg_hdr)];
  339. /*
  340. * Stash away this global state for completing the
  341. * transaction; note transactions are serialized.
  342. */
  343. kvp_transaction.recv_len = recvlen;
  344. kvp_transaction.recv_channel = channel;
  345. kvp_transaction.recv_req_id = requestid;
  346. kvp_transaction.active = true;
  347. kvp_transaction.kvp_msg = kvp_msg;
  348. /*
  349. * Get the information from the
  350. * user-mode component.
  351. * component. This transaction will be
  352. * completed when we get the value from
  353. * the user-mode component.
  354. * Set a timeout to deal with
  355. * user-mode not responding.
  356. */
  357. schedule_work(&kvp_sendkey_work);
  358. schedule_delayed_work(&kvp_work, 5*HZ);
  359. return;
  360. }
  361. icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
  362. | ICMSGHDRFLAG_RESPONSE;
  363. vmbus_sendpacket(channel, recv_buffer,
  364. recvlen, requestid,
  365. VM_PKT_DATA_INBAND, 0);
  366. }
  367. }
  368. int
  369. hv_kvp_init(struct hv_util_service *srv)
  370. {
  371. int err;
  372. err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
  373. if (err)
  374. return err;
  375. recv_buffer = srv->recv_buffer;
  376. /*
  377. * When this driver loads, the user level daemon that
  378. * processes the host requests may not yet be running.
  379. * Defer processing channel callbacks until the daemon
  380. * has registered.
  381. */
  382. kvp_transaction.active = true;
  383. return 0;
  384. }
  385. void hv_kvp_deinit(void)
  386. {
  387. cn_del_callback(&kvp_id);
  388. cancel_delayed_work_sync(&kvp_work);
  389. cancel_work_sync(&kvp_sendkey_work);
  390. }