vmci_datagram.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * VMware VMCI Driver
  3. *
  4. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  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 as published by the
  8. * Free Software Foundation version 2 and no later version.
  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 MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/vmw_vmci_defs.h>
  16. #include <linux/vmw_vmci_api.h>
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/bug.h>
  21. #include "vmci_datagram.h"
  22. #include "vmci_resource.h"
  23. #include "vmci_context.h"
  24. #include "vmci_driver.h"
  25. #include "vmci_event.h"
  26. #include "vmci_route.h"
  27. /*
  28. * struct datagram_entry describes the datagram entity. It is used for datagram
  29. * entities created only on the host.
  30. */
  31. struct datagram_entry {
  32. struct vmci_resource resource;
  33. u32 flags;
  34. bool run_delayed;
  35. vmci_datagram_recv_cb recv_cb;
  36. void *client_data;
  37. u32 priv_flags;
  38. };
  39. struct delayed_datagram_info {
  40. struct datagram_entry *entry;
  41. struct work_struct work;
  42. bool in_dg_host_queue;
  43. /* msg and msg_payload must be together. */
  44. struct vmci_datagram msg;
  45. u8 msg_payload[];
  46. };
  47. /* Number of in-flight host->host datagrams */
  48. static atomic_t delayed_dg_host_queue_size = ATOMIC_INIT(0);
  49. /*
  50. * Create a datagram entry given a handle pointer.
  51. */
  52. static int dg_create_handle(u32 resource_id,
  53. u32 flags,
  54. u32 priv_flags,
  55. vmci_datagram_recv_cb recv_cb,
  56. void *client_data, struct vmci_handle *out_handle)
  57. {
  58. int result;
  59. u32 context_id;
  60. struct vmci_handle handle;
  61. struct datagram_entry *entry;
  62. if ((flags & VMCI_FLAG_WELLKNOWN_DG_HND) != 0)
  63. return VMCI_ERROR_INVALID_ARGS;
  64. if ((flags & VMCI_FLAG_ANYCID_DG_HND) != 0) {
  65. context_id = VMCI_INVALID_ID;
  66. } else {
  67. context_id = vmci_get_context_id();
  68. if (context_id == VMCI_INVALID_ID)
  69. return VMCI_ERROR_NO_RESOURCES;
  70. }
  71. handle = vmci_make_handle(context_id, resource_id);
  72. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  73. if (!entry) {
  74. pr_warn("Failed allocating memory for datagram entry\n");
  75. return VMCI_ERROR_NO_MEM;
  76. }
  77. entry->run_delayed = (flags & VMCI_FLAG_DG_DELAYED_CB) ? true : false;
  78. entry->flags = flags;
  79. entry->recv_cb = recv_cb;
  80. entry->client_data = client_data;
  81. entry->priv_flags = priv_flags;
  82. /* Make datagram resource live. */
  83. result = vmci_resource_add(&entry->resource,
  84. VMCI_RESOURCE_TYPE_DATAGRAM,
  85. handle);
  86. if (result != VMCI_SUCCESS) {
  87. pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d\n",
  88. handle.context, handle.resource, result);
  89. kfree(entry);
  90. return result;
  91. }
  92. *out_handle = vmci_resource_handle(&entry->resource);
  93. return VMCI_SUCCESS;
  94. }
  95. /*
  96. * Internal utility function with the same purpose as
  97. * vmci_datagram_get_priv_flags that also takes a context_id.
  98. */
  99. static int vmci_datagram_get_priv_flags(u32 context_id,
  100. struct vmci_handle handle,
  101. u32 *priv_flags)
  102. {
  103. if (context_id == VMCI_INVALID_ID)
  104. return VMCI_ERROR_INVALID_ARGS;
  105. if (context_id == VMCI_HOST_CONTEXT_ID) {
  106. struct datagram_entry *src_entry;
  107. struct vmci_resource *resource;
  108. resource = vmci_resource_by_handle(handle,
  109. VMCI_RESOURCE_TYPE_DATAGRAM);
  110. if (!resource)
  111. return VMCI_ERROR_INVALID_ARGS;
  112. src_entry = container_of(resource, struct datagram_entry,
  113. resource);
  114. *priv_flags = src_entry->priv_flags;
  115. vmci_resource_put(resource);
  116. } else if (context_id == VMCI_HYPERVISOR_CONTEXT_ID)
  117. *priv_flags = VMCI_MAX_PRIVILEGE_FLAGS;
  118. else
  119. *priv_flags = vmci_context_get_priv_flags(context_id);
  120. return VMCI_SUCCESS;
  121. }
  122. /*
  123. * Calls the specified callback in a delayed context.
  124. */
  125. static void dg_delayed_dispatch(struct work_struct *work)
  126. {
  127. struct delayed_datagram_info *dg_info =
  128. container_of(work, struct delayed_datagram_info, work);
  129. dg_info->entry->recv_cb(dg_info->entry->client_data, &dg_info->msg);
  130. vmci_resource_put(&dg_info->entry->resource);
  131. if (dg_info->in_dg_host_queue)
  132. atomic_dec(&delayed_dg_host_queue_size);
  133. kfree(dg_info);
  134. }
  135. /*
  136. * Dispatch datagram as a host, to the host, or other vm context. This
  137. * function cannot dispatch to hypervisor context handlers. This should
  138. * have been handled before we get here by vmci_datagram_dispatch.
  139. * Returns number of bytes sent on success, error code otherwise.
  140. */
  141. static int dg_dispatch_as_host(u32 context_id, struct vmci_datagram *dg)
  142. {
  143. int retval;
  144. size_t dg_size;
  145. u32 src_priv_flags;
  146. dg_size = VMCI_DG_SIZE(dg);
  147. /* Host cannot send to the hypervisor. */
  148. if (dg->dst.context == VMCI_HYPERVISOR_CONTEXT_ID)
  149. return VMCI_ERROR_DST_UNREACHABLE;
  150. /* Check that source handle matches sending context. */
  151. if (dg->src.context != context_id) {
  152. pr_devel("Sender context (ID=0x%x) is not owner of src datagram entry (handle=0x%x:0x%x)\n",
  153. context_id, dg->src.context, dg->src.resource);
  154. return VMCI_ERROR_NO_ACCESS;
  155. }
  156. /* Get hold of privileges of sending endpoint. */
  157. retval = vmci_datagram_get_priv_flags(context_id, dg->src,
  158. &src_priv_flags);
  159. if (retval != VMCI_SUCCESS) {
  160. pr_warn("Couldn't get privileges (handle=0x%x:0x%x)\n",
  161. dg->src.context, dg->src.resource);
  162. return retval;
  163. }
  164. /* Determine if we should route to host or guest destination. */
  165. if (dg->dst.context == VMCI_HOST_CONTEXT_ID) {
  166. /* Route to host datagram entry. */
  167. struct datagram_entry *dst_entry;
  168. struct vmci_resource *resource;
  169. if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
  170. dg->dst.resource == VMCI_EVENT_HANDLER) {
  171. return vmci_event_dispatch(dg);
  172. }
  173. resource = vmci_resource_by_handle(dg->dst,
  174. VMCI_RESOURCE_TYPE_DATAGRAM);
  175. if (!resource) {
  176. pr_devel("Sending to invalid destination (handle=0x%x:0x%x)\n",
  177. dg->dst.context, dg->dst.resource);
  178. return VMCI_ERROR_INVALID_RESOURCE;
  179. }
  180. dst_entry = container_of(resource, struct datagram_entry,
  181. resource);
  182. if (vmci_deny_interaction(src_priv_flags,
  183. dst_entry->priv_flags)) {
  184. vmci_resource_put(resource);
  185. return VMCI_ERROR_NO_ACCESS;
  186. }
  187. /*
  188. * If a VMCI datagram destined for the host is also sent by the
  189. * host, we always run it delayed. This ensures that no locks
  190. * are held when the datagram callback runs.
  191. */
  192. if (dst_entry->run_delayed ||
  193. dg->src.context == VMCI_HOST_CONTEXT_ID) {
  194. struct delayed_datagram_info *dg_info;
  195. if (atomic_add_return(1, &delayed_dg_host_queue_size)
  196. == VMCI_MAX_DELAYED_DG_HOST_QUEUE_SIZE) {
  197. atomic_dec(&delayed_dg_host_queue_size);
  198. vmci_resource_put(resource);
  199. return VMCI_ERROR_NO_MEM;
  200. }
  201. dg_info = kmalloc(sizeof(*dg_info) +
  202. (size_t) dg->payload_size, GFP_ATOMIC);
  203. if (!dg_info) {
  204. atomic_dec(&delayed_dg_host_queue_size);
  205. vmci_resource_put(resource);
  206. return VMCI_ERROR_NO_MEM;
  207. }
  208. dg_info->in_dg_host_queue = true;
  209. dg_info->entry = dst_entry;
  210. memcpy(&dg_info->msg, dg, dg_size);
  211. INIT_WORK(&dg_info->work, dg_delayed_dispatch);
  212. schedule_work(&dg_info->work);
  213. retval = VMCI_SUCCESS;
  214. } else {
  215. retval = dst_entry->recv_cb(dst_entry->client_data, dg);
  216. vmci_resource_put(resource);
  217. if (retval < VMCI_SUCCESS)
  218. return retval;
  219. }
  220. } else {
  221. /* Route to destination VM context. */
  222. struct vmci_datagram *new_dg;
  223. if (context_id != dg->dst.context) {
  224. if (vmci_deny_interaction(src_priv_flags,
  225. vmci_context_get_priv_flags
  226. (dg->dst.context))) {
  227. return VMCI_ERROR_NO_ACCESS;
  228. } else if (VMCI_CONTEXT_IS_VM(context_id)) {
  229. /*
  230. * If the sending context is a VM, it
  231. * cannot reach another VM.
  232. */
  233. pr_devel("Datagram communication between VMs not supported (src=0x%x, dst=0x%x)\n",
  234. context_id, dg->dst.context);
  235. return VMCI_ERROR_DST_UNREACHABLE;
  236. }
  237. }
  238. /* We make a copy to enqueue. */
  239. new_dg = kmemdup(dg, dg_size, GFP_KERNEL);
  240. if (new_dg == NULL)
  241. return VMCI_ERROR_NO_MEM;
  242. retval = vmci_ctx_enqueue_datagram(dg->dst.context, new_dg);
  243. if (retval < VMCI_SUCCESS) {
  244. kfree(new_dg);
  245. return retval;
  246. }
  247. }
  248. /*
  249. * We currently truncate the size to signed 32 bits. This doesn't
  250. * matter for this handler as it only support 4Kb messages.
  251. */
  252. return (int)dg_size;
  253. }
  254. /*
  255. * Dispatch datagram as a guest, down through the VMX and potentially to
  256. * the host.
  257. * Returns number of bytes sent on success, error code otherwise.
  258. */
  259. static int dg_dispatch_as_guest(struct vmci_datagram *dg)
  260. {
  261. int retval;
  262. struct vmci_resource *resource;
  263. resource = vmci_resource_by_handle(dg->src,
  264. VMCI_RESOURCE_TYPE_DATAGRAM);
  265. if (!resource)
  266. return VMCI_ERROR_NO_HANDLE;
  267. retval = vmci_send_datagram(dg);
  268. vmci_resource_put(resource);
  269. return retval;
  270. }
  271. /*
  272. * Dispatch datagram. This will determine the routing for the datagram
  273. * and dispatch it accordingly.
  274. * Returns number of bytes sent on success, error code otherwise.
  275. */
  276. int vmci_datagram_dispatch(u32 context_id,
  277. struct vmci_datagram *dg, bool from_guest)
  278. {
  279. int retval;
  280. enum vmci_route route;
  281. BUILD_BUG_ON(sizeof(struct vmci_datagram) != 24);
  282. if (dg->payload_size > VMCI_MAX_DG_SIZE ||
  283. VMCI_DG_SIZE(dg) > VMCI_MAX_DG_SIZE) {
  284. pr_devel("Payload (size=%llu bytes) too big to send\n",
  285. (unsigned long long)dg->payload_size);
  286. return VMCI_ERROR_INVALID_ARGS;
  287. }
  288. retval = vmci_route(&dg->src, &dg->dst, from_guest, &route);
  289. if (retval < VMCI_SUCCESS) {
  290. pr_devel("Failed to route datagram (src=0x%x, dst=0x%x, err=%d)\n",
  291. dg->src.context, dg->dst.context, retval);
  292. return retval;
  293. }
  294. if (VMCI_ROUTE_AS_HOST == route) {
  295. if (VMCI_INVALID_ID == context_id)
  296. context_id = VMCI_HOST_CONTEXT_ID;
  297. return dg_dispatch_as_host(context_id, dg);
  298. }
  299. if (VMCI_ROUTE_AS_GUEST == route)
  300. return dg_dispatch_as_guest(dg);
  301. pr_warn("Unknown route (%d) for datagram\n", route);
  302. return VMCI_ERROR_DST_UNREACHABLE;
  303. }
  304. /*
  305. * Invoke the handler for the given datagram. This is intended to be
  306. * called only when acting as a guest and receiving a datagram from the
  307. * virtual device.
  308. */
  309. int vmci_datagram_invoke_guest_handler(struct vmci_datagram *dg)
  310. {
  311. struct vmci_resource *resource;
  312. struct datagram_entry *dst_entry;
  313. resource = vmci_resource_by_handle(dg->dst,
  314. VMCI_RESOURCE_TYPE_DATAGRAM);
  315. if (!resource) {
  316. pr_devel("destination (handle=0x%x:0x%x) doesn't exist\n",
  317. dg->dst.context, dg->dst.resource);
  318. return VMCI_ERROR_NO_HANDLE;
  319. }
  320. dst_entry = container_of(resource, struct datagram_entry, resource);
  321. if (dst_entry->run_delayed) {
  322. struct delayed_datagram_info *dg_info;
  323. dg_info = kmalloc(sizeof(*dg_info) + (size_t)dg->payload_size,
  324. GFP_ATOMIC);
  325. if (!dg_info) {
  326. vmci_resource_put(resource);
  327. return VMCI_ERROR_NO_MEM;
  328. }
  329. dg_info->in_dg_host_queue = false;
  330. dg_info->entry = dst_entry;
  331. memcpy(&dg_info->msg, dg, VMCI_DG_SIZE(dg));
  332. INIT_WORK(&dg_info->work, dg_delayed_dispatch);
  333. schedule_work(&dg_info->work);
  334. } else {
  335. dst_entry->recv_cb(dst_entry->client_data, dg);
  336. vmci_resource_put(resource);
  337. }
  338. return VMCI_SUCCESS;
  339. }
  340. /*
  341. * vmci_datagram_create_handle_priv() - Create host context datagram endpoint
  342. * @resource_id: The resource ID.
  343. * @flags: Datagram Flags.
  344. * @priv_flags: Privilege Flags.
  345. * @recv_cb: Callback when receiving datagrams.
  346. * @client_data: Pointer for a datagram_entry struct
  347. * @out_handle: vmci_handle that is populated as a result of this function.
  348. *
  349. * Creates a host context datagram endpoint and returns a handle to it.
  350. */
  351. int vmci_datagram_create_handle_priv(u32 resource_id,
  352. u32 flags,
  353. u32 priv_flags,
  354. vmci_datagram_recv_cb recv_cb,
  355. void *client_data,
  356. struct vmci_handle *out_handle)
  357. {
  358. if (out_handle == NULL)
  359. return VMCI_ERROR_INVALID_ARGS;
  360. if (recv_cb == NULL) {
  361. pr_devel("Client callback needed when creating datagram\n");
  362. return VMCI_ERROR_INVALID_ARGS;
  363. }
  364. if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS)
  365. return VMCI_ERROR_INVALID_ARGS;
  366. return dg_create_handle(resource_id, flags, priv_flags, recv_cb,
  367. client_data, out_handle);
  368. }
  369. EXPORT_SYMBOL_GPL(vmci_datagram_create_handle_priv);
  370. /*
  371. * vmci_datagram_create_handle() - Create host context datagram endpoint
  372. * @resource_id: Resource ID.
  373. * @flags: Datagram Flags.
  374. * @recv_cb: Callback when receiving datagrams.
  375. * @client_ata: Pointer for a datagram_entry struct
  376. * @out_handle: vmci_handle that is populated as a result of this function.
  377. *
  378. * Creates a host context datagram endpoint and returns a handle to
  379. * it. Same as vmci_datagram_create_handle_priv without the priviledge
  380. * flags argument.
  381. */
  382. int vmci_datagram_create_handle(u32 resource_id,
  383. u32 flags,
  384. vmci_datagram_recv_cb recv_cb,
  385. void *client_data,
  386. struct vmci_handle *out_handle)
  387. {
  388. return vmci_datagram_create_handle_priv(
  389. resource_id, flags,
  390. VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
  391. recv_cb, client_data,
  392. out_handle);
  393. }
  394. EXPORT_SYMBOL_GPL(vmci_datagram_create_handle);
  395. /*
  396. * vmci_datagram_destroy_handle() - Destroys datagram handle
  397. * @handle: vmci_handle to be destroyed and reaped.
  398. *
  399. * Use this function to destroy any datagram handles created by
  400. * vmci_datagram_create_handle{,Priv} functions.
  401. */
  402. int vmci_datagram_destroy_handle(struct vmci_handle handle)
  403. {
  404. struct datagram_entry *entry;
  405. struct vmci_resource *resource;
  406. resource = vmci_resource_by_handle(handle, VMCI_RESOURCE_TYPE_DATAGRAM);
  407. if (!resource) {
  408. pr_devel("Failed to destroy datagram (handle=0x%x:0x%x)\n",
  409. handle.context, handle.resource);
  410. return VMCI_ERROR_NOT_FOUND;
  411. }
  412. entry = container_of(resource, struct datagram_entry, resource);
  413. vmci_resource_put(&entry->resource);
  414. vmci_resource_remove(&entry->resource);
  415. kfree(entry);
  416. return VMCI_SUCCESS;
  417. }
  418. EXPORT_SYMBOL_GPL(vmci_datagram_destroy_handle);
  419. /*
  420. * vmci_datagram_send() - Send a datagram
  421. * @msg: The datagram to send.
  422. *
  423. * Sends the provided datagram on its merry way.
  424. */
  425. int vmci_datagram_send(struct vmci_datagram *msg)
  426. {
  427. if (msg == NULL)
  428. return VMCI_ERROR_INVALID_ARGS;
  429. return vmci_datagram_dispatch(VMCI_INVALID_ID, msg, false);
  430. }
  431. EXPORT_SYMBOL_GPL(vmci_datagram_send);