4 * Copyright (C) 2012 VMware, Inc. All rights reserved.
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.
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
16 #include <linux/vmw_vmci_defs.h>
17 #include <linux/vmw_vmci_api.h>
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/bug.h>
23 #include "vmci_datagram.h"
24 #include "vmci_resource.h"
25 #include "vmci_context.h"
26 #include "vmci_driver.h"
27 #include "vmci_event.h"
28 #include "vmci_route.h"
31 * struct datagram_entry describes the datagram entity. It is used for datagram
32 * entities created only on the host.
34 struct datagram_entry
{
35 struct vmci_resource resource
;
38 vmci_datagram_recv_cb recv_cb
;
43 struct delayed_datagram_info
{
44 struct datagram_entry
*entry
;
45 struct work_struct work
;
46 bool in_dg_host_queue
;
47 /* msg and msg_payload must be together. */
48 struct vmci_datagram msg
;
52 /* Number of in-flight host->host datagrams */
53 static atomic_t delayed_dg_host_queue_size
= ATOMIC_INIT(0);
56 * Create a datagram entry given a handle pointer.
58 static int dg_create_handle(u32 resource_id
,
61 vmci_datagram_recv_cb recv_cb
,
62 void *client_data
, struct vmci_handle
*out_handle
)
66 struct vmci_handle handle
;
67 struct datagram_entry
*entry
;
69 if ((flags
& VMCI_FLAG_WELLKNOWN_DG_HND
) != 0)
70 return VMCI_ERROR_INVALID_ARGS
;
72 if ((flags
& VMCI_FLAG_ANYCID_DG_HND
) != 0) {
73 context_id
= VMCI_INVALID_ID
;
75 context_id
= vmci_get_context_id();
76 if (context_id
== VMCI_INVALID_ID
)
77 return VMCI_ERROR_NO_RESOURCES
;
80 handle
= vmci_make_handle(context_id
, resource_id
);
82 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
84 pr_warn("Failed allocating memory for datagram entry\n");
85 return VMCI_ERROR_NO_MEM
;
88 entry
->run_delayed
= (flags
& VMCI_FLAG_DG_DELAYED_CB
) ? true : false;
90 entry
->recv_cb
= recv_cb
;
91 entry
->client_data
= client_data
;
92 entry
->priv_flags
= priv_flags
;
94 /* Make datagram resource live. */
95 result
= vmci_resource_add(&entry
->resource
,
96 VMCI_RESOURCE_TYPE_DATAGRAM
,
98 if (result
!= VMCI_SUCCESS
) {
99 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d\n",
100 handle
.context
, handle
.resource
, result
);
105 *out_handle
= vmci_resource_handle(&entry
->resource
);
110 * Internal utility function with the same purpose as
111 * vmci_datagram_get_priv_flags that also takes a context_id.
113 static int vmci_datagram_get_priv_flags(u32 context_id
,
114 struct vmci_handle handle
,
117 if (context_id
== VMCI_INVALID_ID
)
118 return VMCI_ERROR_INVALID_ARGS
;
120 if (context_id
== VMCI_HOST_CONTEXT_ID
) {
121 struct datagram_entry
*src_entry
;
122 struct vmci_resource
*resource
;
124 resource
= vmci_resource_by_handle(handle
,
125 VMCI_RESOURCE_TYPE_DATAGRAM
);
127 return VMCI_ERROR_INVALID_ARGS
;
129 src_entry
= container_of(resource
, struct datagram_entry
,
131 *priv_flags
= src_entry
->priv_flags
;
132 vmci_resource_put(resource
);
133 } else if (context_id
== VMCI_HYPERVISOR_CONTEXT_ID
)
134 *priv_flags
= VMCI_MAX_PRIVILEGE_FLAGS
;
136 *priv_flags
= vmci_context_get_priv_flags(context_id
);
142 * Calls the specified callback in a delayed context.
144 static void dg_delayed_dispatch(struct work_struct
*work
)
146 struct delayed_datagram_info
*dg_info
=
147 container_of(work
, struct delayed_datagram_info
, work
);
149 dg_info
->entry
->recv_cb(dg_info
->entry
->client_data
, &dg_info
->msg
);
151 vmci_resource_put(&dg_info
->entry
->resource
);
153 if (dg_info
->in_dg_host_queue
)
154 atomic_dec(&delayed_dg_host_queue_size
);
160 * Dispatch datagram as a host, to the host, or other vm context. This
161 * function cannot dispatch to hypervisor context handlers. This should
162 * have been handled before we get here by vmci_datagram_dispatch.
163 * Returns number of bytes sent on success, error code otherwise.
165 static int dg_dispatch_as_host(u32 context_id
, struct vmci_datagram
*dg
)
171 dg_size
= VMCI_DG_SIZE(dg
);
173 /* Host cannot send to the hypervisor. */
174 if (dg
->dst
.context
== VMCI_HYPERVISOR_CONTEXT_ID
)
175 return VMCI_ERROR_DST_UNREACHABLE
;
177 /* Check that source handle matches sending context. */
178 if (dg
->src
.context
!= context_id
) {
179 pr_devel("Sender context (ID=0x%x) is not owner of src datagram entry (handle=0x%x:0x%x)\n",
180 context_id
, dg
->src
.context
, dg
->src
.resource
);
181 return VMCI_ERROR_NO_ACCESS
;
184 /* Get hold of privileges of sending endpoint. */
185 retval
= vmci_datagram_get_priv_flags(context_id
, dg
->src
,
187 if (retval
!= VMCI_SUCCESS
) {
188 pr_warn("Couldn't get privileges (handle=0x%x:0x%x)\n",
189 dg
->src
.context
, dg
->src
.resource
);
193 /* Determine if we should route to host or guest destination. */
194 if (dg
->dst
.context
== VMCI_HOST_CONTEXT_ID
) {
195 /* Route to host datagram entry. */
196 struct datagram_entry
*dst_entry
;
197 struct vmci_resource
*resource
;
199 if (dg
->src
.context
== VMCI_HYPERVISOR_CONTEXT_ID
&&
200 dg
->dst
.resource
== VMCI_EVENT_HANDLER
) {
201 return vmci_event_dispatch(dg
);
204 resource
= vmci_resource_by_handle(dg
->dst
,
205 VMCI_RESOURCE_TYPE_DATAGRAM
);
207 pr_devel("Sending to invalid destination (handle=0x%x:0x%x)\n",
208 dg
->dst
.context
, dg
->dst
.resource
);
209 return VMCI_ERROR_INVALID_RESOURCE
;
211 dst_entry
= container_of(resource
, struct datagram_entry
,
213 if (vmci_deny_interaction(src_priv_flags
,
214 dst_entry
->priv_flags
)) {
215 vmci_resource_put(resource
);
216 return VMCI_ERROR_NO_ACCESS
;
220 * If a VMCI datagram destined for the host is also sent by the
221 * host, we always run it delayed. This ensures that no locks
222 * are held when the datagram callback runs.
224 if (dst_entry
->run_delayed
||
225 dg
->src
.context
== VMCI_HOST_CONTEXT_ID
) {
226 struct delayed_datagram_info
*dg_info
;
228 if (atomic_add_return(1, &delayed_dg_host_queue_size
)
229 == VMCI_MAX_DELAYED_DG_HOST_QUEUE_SIZE
) {
230 atomic_dec(&delayed_dg_host_queue_size
);
231 vmci_resource_put(resource
);
232 return VMCI_ERROR_NO_MEM
;
235 dg_info
= kmalloc(sizeof(*dg_info
) +
236 (size_t) dg
->payload_size
, GFP_ATOMIC
);
238 atomic_dec(&delayed_dg_host_queue_size
);
239 vmci_resource_put(resource
);
240 return VMCI_ERROR_NO_MEM
;
243 dg_info
->in_dg_host_queue
= true;
244 dg_info
->entry
= dst_entry
;
245 memcpy(&dg_info
->msg
, dg
, dg_size
);
247 INIT_WORK(&dg_info
->work
, dg_delayed_dispatch
);
248 schedule_work(&dg_info
->work
);
249 retval
= VMCI_SUCCESS
;
252 retval
= dst_entry
->recv_cb(dst_entry
->client_data
, dg
);
253 vmci_resource_put(resource
);
254 if (retval
< VMCI_SUCCESS
)
258 /* Route to destination VM context. */
259 struct vmci_datagram
*new_dg
;
261 if (context_id
!= dg
->dst
.context
) {
262 if (vmci_deny_interaction(src_priv_flags
,
263 vmci_context_get_priv_flags
264 (dg
->dst
.context
))) {
265 return VMCI_ERROR_NO_ACCESS
;
266 } else if (VMCI_CONTEXT_IS_VM(context_id
)) {
268 * If the sending context is a VM, it
269 * cannot reach another VM.
272 pr_devel("Datagram communication between VMs not supported (src=0x%x, dst=0x%x)\n",
273 context_id
, dg
->dst
.context
);
274 return VMCI_ERROR_DST_UNREACHABLE
;
278 /* We make a copy to enqueue. */
279 new_dg
= kmalloc(dg_size
, GFP_KERNEL
);
281 return VMCI_ERROR_NO_MEM
;
283 memcpy(new_dg
, dg
, dg_size
);
284 retval
= vmci_ctx_enqueue_datagram(dg
->dst
.context
, new_dg
);
285 if (retval
< VMCI_SUCCESS
) {
292 * We currently truncate the size to signed 32 bits. This doesn't
293 * matter for this handler as it only support 4Kb messages.
299 * Dispatch datagram as a guest, down through the VMX and potentially to
301 * Returns number of bytes sent on success, error code otherwise.
303 static int dg_dispatch_as_guest(struct vmci_datagram
*dg
)
306 struct vmci_resource
*resource
;
308 resource
= vmci_resource_by_handle(dg
->src
,
309 VMCI_RESOURCE_TYPE_DATAGRAM
);
311 return VMCI_ERROR_NO_HANDLE
;
313 retval
= vmci_send_datagram(dg
);
314 vmci_resource_put(resource
);
319 * Dispatch datagram. This will determine the routing for the datagram
320 * and dispatch it accordingly.
321 * Returns number of bytes sent on success, error code otherwise.
323 int vmci_datagram_dispatch(u32 context_id
,
324 struct vmci_datagram
*dg
, bool from_guest
)
327 enum vmci_route route
;
329 BUILD_BUG_ON(sizeof(struct vmci_datagram
) != 24);
331 if (VMCI_DG_SIZE(dg
) > VMCI_MAX_DG_SIZE
) {
332 pr_devel("Payload (size=%llu bytes) too big to send\n",
333 (unsigned long long)dg
->payload_size
);
334 return VMCI_ERROR_INVALID_ARGS
;
337 retval
= vmci_route(&dg
->src
, &dg
->dst
, from_guest
, &route
);
338 if (retval
< VMCI_SUCCESS
) {
339 pr_devel("Failed to route datagram (src=0x%x, dst=0x%x, err=%d)\n",
340 dg
->src
.context
, dg
->dst
.context
, retval
);
344 if (VMCI_ROUTE_AS_HOST
== route
) {
345 if (VMCI_INVALID_ID
== context_id
)
346 context_id
= VMCI_HOST_CONTEXT_ID
;
347 return dg_dispatch_as_host(context_id
, dg
);
350 if (VMCI_ROUTE_AS_GUEST
== route
)
351 return dg_dispatch_as_guest(dg
);
353 pr_warn("Unknown route (%d) for datagram\n", route
);
354 return VMCI_ERROR_DST_UNREACHABLE
;
358 * Invoke the handler for the given datagram. This is intended to be
359 * called only when acting as a guest and receiving a datagram from the
362 int vmci_datagram_invoke_guest_handler(struct vmci_datagram
*dg
)
364 struct vmci_resource
*resource
;
365 struct datagram_entry
*dst_entry
;
367 resource
= vmci_resource_by_handle(dg
->dst
,
368 VMCI_RESOURCE_TYPE_DATAGRAM
);
370 pr_devel("destination (handle=0x%x:0x%x) doesn't exist\n",
371 dg
->dst
.context
, dg
->dst
.resource
);
372 return VMCI_ERROR_NO_HANDLE
;
375 dst_entry
= container_of(resource
, struct datagram_entry
, resource
);
376 if (dst_entry
->run_delayed
) {
377 struct delayed_datagram_info
*dg_info
;
379 dg_info
= kmalloc(sizeof(*dg_info
) + (size_t)dg
->payload_size
,
382 vmci_resource_put(resource
);
383 return VMCI_ERROR_NO_MEM
;
386 dg_info
->in_dg_host_queue
= false;
387 dg_info
->entry
= dst_entry
;
388 memcpy(&dg_info
->msg
, dg
, VMCI_DG_SIZE(dg
));
390 INIT_WORK(&dg_info
->work
, dg_delayed_dispatch
);
391 schedule_work(&dg_info
->work
);
393 dst_entry
->recv_cb(dst_entry
->client_data
, dg
);
394 vmci_resource_put(resource
);
401 * vmci_datagram_create_handle_priv() - Create host context datagram endpoint
402 * @resource_id: The resource ID.
403 * @flags: Datagram Flags.
404 * @priv_flags: Privilege Flags.
405 * @recv_cb: Callback when receiving datagrams.
406 * @client_data: Pointer for a datagram_entry struct
407 * @out_handle: vmci_handle that is populated as a result of this function.
409 * Creates a host context datagram endpoint and returns a handle to it.
411 int vmci_datagram_create_handle_priv(u32 resource_id
,
414 vmci_datagram_recv_cb recv_cb
,
416 struct vmci_handle
*out_handle
)
418 if (out_handle
== NULL
)
419 return VMCI_ERROR_INVALID_ARGS
;
421 if (recv_cb
== NULL
) {
422 pr_devel("Client callback needed when creating datagram\n");
423 return VMCI_ERROR_INVALID_ARGS
;
426 if (priv_flags
& ~VMCI_PRIVILEGE_ALL_FLAGS
)
427 return VMCI_ERROR_INVALID_ARGS
;
429 return dg_create_handle(resource_id
, flags
, priv_flags
, recv_cb
,
430 client_data
, out_handle
);
432 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle_priv
);
435 * vmci_datagram_create_handle() - Create host context datagram endpoint
436 * @resource_id: Resource ID.
437 * @flags: Datagram Flags.
438 * @recv_cb: Callback when receiving datagrams.
439 * @client_ata: Pointer for a datagram_entry struct
440 * @out_handle: vmci_handle that is populated as a result of this function.
442 * Creates a host context datagram endpoint and returns a handle to
443 * it. Same as vmci_datagram_create_handle_priv without the priviledge
446 int vmci_datagram_create_handle(u32 resource_id
,
448 vmci_datagram_recv_cb recv_cb
,
450 struct vmci_handle
*out_handle
)
452 return vmci_datagram_create_handle_priv(
454 VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS
,
455 recv_cb
, client_data
,
458 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle
);
461 * vmci_datagram_destroy_handle() - Destroys datagram handle
462 * @handle: vmci_handle to be destroyed and reaped.
464 * Use this function to destroy any datagram handles created by
465 * vmci_datagram_create_handle{,Priv} functions.
467 int vmci_datagram_destroy_handle(struct vmci_handle handle
)
469 struct datagram_entry
*entry
;
470 struct vmci_resource
*resource
;
472 resource
= vmci_resource_by_handle(handle
, VMCI_RESOURCE_TYPE_DATAGRAM
);
474 pr_devel("Failed to destroy datagram (handle=0x%x:0x%x)\n",
475 handle
.context
, handle
.resource
);
476 return VMCI_ERROR_NOT_FOUND
;
479 entry
= container_of(resource
, struct datagram_entry
, resource
);
481 vmci_resource_put(&entry
->resource
);
482 vmci_resource_remove(&entry
->resource
);
487 EXPORT_SYMBOL_GPL(vmci_datagram_destroy_handle
);
490 * vmci_datagram_send() - Send a datagram
491 * @msg: The datagram to send.
493 * Sends the provided datagram on its merry way.
495 int vmci_datagram_send(struct vmci_datagram
*msg
)
498 return VMCI_ERROR_INVALID_ARGS
;
500 return vmci_datagram_dispatch(VMCI_INVALID_ID
, msg
, false);
502 EXPORT_SYMBOL_GPL(vmci_datagram_send
);