hv_utils_transport.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Kernel/userspace transport abstraction for Hyper-V util driver.
  3. *
  4. * Copyright (C) 2015, Vitaly Kuznetsov <vkuznets@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13. * NON INFRINGEMENT. See the GNU General Public License for more
  14. * details.
  15. *
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/poll.h>
  20. #include "hyperv_vmbus.h"
  21. #include "hv_utils_transport.h"
  22. static DEFINE_SPINLOCK(hvt_list_lock);
  23. static struct list_head hvt_list = LIST_HEAD_INIT(hvt_list);
  24. static void hvt_reset(struct hvutil_transport *hvt)
  25. {
  26. mutex_lock(&hvt->outmsg_lock);
  27. kfree(hvt->outmsg);
  28. hvt->outmsg = NULL;
  29. hvt->outmsg_len = 0;
  30. mutex_unlock(&hvt->outmsg_lock);
  31. if (hvt->on_reset)
  32. hvt->on_reset();
  33. }
  34. static ssize_t hvt_op_read(struct file *file, char __user *buf,
  35. size_t count, loff_t *ppos)
  36. {
  37. struct hvutil_transport *hvt;
  38. int ret;
  39. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  40. if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0))
  41. return -EINTR;
  42. mutex_lock(&hvt->outmsg_lock);
  43. if (!hvt->outmsg) {
  44. ret = -EAGAIN;
  45. goto out_unlock;
  46. }
  47. if (count < hvt->outmsg_len) {
  48. ret = -EINVAL;
  49. goto out_unlock;
  50. }
  51. if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
  52. ret = hvt->outmsg_len;
  53. else
  54. ret = -EFAULT;
  55. kfree(hvt->outmsg);
  56. hvt->outmsg = NULL;
  57. hvt->outmsg_len = 0;
  58. out_unlock:
  59. mutex_unlock(&hvt->outmsg_lock);
  60. return ret;
  61. }
  62. static ssize_t hvt_op_write(struct file *file, const char __user *buf,
  63. size_t count, loff_t *ppos)
  64. {
  65. struct hvutil_transport *hvt;
  66. u8 *inmsg;
  67. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  68. inmsg = kzalloc(count, GFP_KERNEL);
  69. if (copy_from_user(inmsg, buf, count)) {
  70. kfree(inmsg);
  71. return -EFAULT;
  72. }
  73. if (hvt->on_msg(inmsg, count))
  74. return -EFAULT;
  75. kfree(inmsg);
  76. return count;
  77. }
  78. static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
  79. {
  80. struct hvutil_transport *hvt;
  81. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  82. poll_wait(file, &hvt->outmsg_q, wait);
  83. if (hvt->outmsg_len > 0)
  84. return POLLIN | POLLRDNORM;
  85. return 0;
  86. }
  87. static int hvt_op_open(struct inode *inode, struct file *file)
  88. {
  89. struct hvutil_transport *hvt;
  90. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  91. /*
  92. * Switching to CHARDEV mode. We switch bach to INIT when device
  93. * gets released.
  94. */
  95. if (hvt->mode == HVUTIL_TRANSPORT_INIT)
  96. hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
  97. else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
  98. /*
  99. * We're switching from netlink communication to using char
  100. * device. Issue the reset first.
  101. */
  102. hvt_reset(hvt);
  103. hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
  104. } else
  105. return -EBUSY;
  106. return 0;
  107. }
  108. static int hvt_op_release(struct inode *inode, struct file *file)
  109. {
  110. struct hvutil_transport *hvt;
  111. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  112. hvt->mode = HVUTIL_TRANSPORT_INIT;
  113. /*
  114. * Cleanup message buffers to avoid spurious messages when the daemon
  115. * connects back.
  116. */
  117. hvt_reset(hvt);
  118. return 0;
  119. }
  120. static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
  121. {
  122. struct hvutil_transport *hvt, *hvt_found = NULL;
  123. spin_lock(&hvt_list_lock);
  124. list_for_each_entry(hvt, &hvt_list, list) {
  125. if (hvt->cn_id.idx == msg->id.idx &&
  126. hvt->cn_id.val == msg->id.val) {
  127. hvt_found = hvt;
  128. break;
  129. }
  130. }
  131. spin_unlock(&hvt_list_lock);
  132. if (!hvt_found) {
  133. pr_warn("hvt_cn_callback: spurious message received!\n");
  134. return;
  135. }
  136. /*
  137. * Switching to NETLINK mode. Switching to CHARDEV happens when someone
  138. * opens the device.
  139. */
  140. if (hvt->mode == HVUTIL_TRANSPORT_INIT)
  141. hvt->mode = HVUTIL_TRANSPORT_NETLINK;
  142. if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
  143. hvt_found->on_msg(msg->data, msg->len);
  144. else
  145. pr_warn("hvt_cn_callback: unexpected netlink message!\n");
  146. }
  147. int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
  148. {
  149. struct cn_msg *cn_msg;
  150. int ret = 0;
  151. if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
  152. return -EINVAL;
  153. } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
  154. cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
  155. if (!cn_msg)
  156. return -ENOMEM;
  157. cn_msg->id.idx = hvt->cn_id.idx;
  158. cn_msg->id.val = hvt->cn_id.val;
  159. cn_msg->len = len;
  160. memcpy(cn_msg->data, msg, len);
  161. ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
  162. kfree(cn_msg);
  163. return ret;
  164. }
  165. /* HVUTIL_TRANSPORT_CHARDEV */
  166. mutex_lock(&hvt->outmsg_lock);
  167. if (hvt->outmsg) {
  168. /* Previous message wasn't received */
  169. ret = -EFAULT;
  170. goto out_unlock;
  171. }
  172. hvt->outmsg = kzalloc(len, GFP_KERNEL);
  173. if (hvt->outmsg) {
  174. memcpy(hvt->outmsg, msg, len);
  175. hvt->outmsg_len = len;
  176. wake_up_interruptible(&hvt->outmsg_q);
  177. } else
  178. ret = -ENOMEM;
  179. out_unlock:
  180. mutex_unlock(&hvt->outmsg_lock);
  181. return ret;
  182. }
  183. struct hvutil_transport *hvutil_transport_init(const char *name,
  184. u32 cn_idx, u32 cn_val,
  185. int (*on_msg)(void *, int),
  186. void (*on_reset)(void))
  187. {
  188. struct hvutil_transport *hvt;
  189. hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
  190. if (!hvt)
  191. return NULL;
  192. hvt->cn_id.idx = cn_idx;
  193. hvt->cn_id.val = cn_val;
  194. hvt->mdev.minor = MISC_DYNAMIC_MINOR;
  195. hvt->mdev.name = name;
  196. hvt->fops.owner = THIS_MODULE;
  197. hvt->fops.read = hvt_op_read;
  198. hvt->fops.write = hvt_op_write;
  199. hvt->fops.poll = hvt_op_poll;
  200. hvt->fops.open = hvt_op_open;
  201. hvt->fops.release = hvt_op_release;
  202. hvt->mdev.fops = &hvt->fops;
  203. init_waitqueue_head(&hvt->outmsg_q);
  204. mutex_init(&hvt->outmsg_lock);
  205. spin_lock(&hvt_list_lock);
  206. list_add(&hvt->list, &hvt_list);
  207. spin_unlock(&hvt_list_lock);
  208. hvt->on_msg = on_msg;
  209. hvt->on_reset = on_reset;
  210. if (misc_register(&hvt->mdev))
  211. goto err_free_hvt;
  212. /* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
  213. if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
  214. cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
  215. goto err_free_hvt;
  216. return hvt;
  217. err_free_hvt:
  218. kfree(hvt);
  219. return NULL;
  220. }
  221. void hvutil_transport_destroy(struct hvutil_transport *hvt)
  222. {
  223. spin_lock(&hvt_list_lock);
  224. list_del(&hvt->list);
  225. spin_unlock(&hvt_list_lock);
  226. if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
  227. cn_del_callback(&hvt->cn_id);
  228. misc_deregister(&hvt->mdev);
  229. kfree(hvt->outmsg);
  230. kfree(hvt);
  231. }