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 vmci_datagram msg
;
46 struct work_struct work
;
47 bool in_dg_host_queue
;
50 /* Number of in-flight host->host datagrams */
51 static atomic_t delayed_dg_host_queue_size
= ATOMIC_INIT(0);
54 * Create a datagram entry given a handle pointer.
56 static int dg_create_handle(u32 resource_id
,
59 vmci_datagram_recv_cb recv_cb
,
60 void *client_data
, struct vmci_handle
*out_handle
)
64 struct vmci_handle handle
;
65 struct datagram_entry
*entry
;
67 if ((flags
& VMCI_FLAG_WELLKNOWN_DG_HND
) != 0)
68 return VMCI_ERROR_INVALID_ARGS
;
70 if ((flags
& VMCI_FLAG_ANYCID_DG_HND
) != 0) {
71 context_id
= VMCI_INVALID_ID
;
73 context_id
= vmci_get_context_id();
74 if (context_id
== VMCI_INVALID_ID
)
75 return VMCI_ERROR_NO_RESOURCES
;
78 handle
= vmci_make_handle(context_id
, resource_id
);
80 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
82 pr_warn("Failed allocating memory for datagram entry\n");
83 return VMCI_ERROR_NO_MEM
;
86 entry
->run_delayed
= (flags
& VMCI_FLAG_DG_DELAYED_CB
) ? true : false;
88 entry
->recv_cb
= recv_cb
;
89 entry
->client_data
= client_data
;
90 entry
->priv_flags
= priv_flags
;
92 /* Make datagram resource live. */
93 result
= vmci_resource_add(&entry
->resource
,
94 VMCI_RESOURCE_TYPE_DATAGRAM
,
96 if (result
!= VMCI_SUCCESS
) {
97 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d\n",
98 handle
.context
, handle
.resource
, result
);
103 *out_handle
= vmci_resource_handle(&entry
->resource
);
108 * Internal utility function with the same purpose as
109 * vmci_datagram_get_priv_flags that also takes a context_id.
111 static int vmci_datagram_get_priv_flags(u32 context_id
,
112 struct vmci_handle handle
,
115 if (context_id
== VMCI_INVALID_ID
)
116 return VMCI_ERROR_INVALID_ARGS
;
118 if (context_id
== VMCI_HOST_CONTEXT_ID
) {
119 struct datagram_entry
*src_entry
;
120 struct vmci_resource
*resource
;
122 resource
= vmci_resource_by_handle(handle
,
123 VMCI_RESOURCE_TYPE_DATAGRAM
);
125 return VMCI_ERROR_INVALID_ARGS
;
127 src_entry
= container_of(resource
, struct datagram_entry
,
129 *priv_flags
= src_entry
->priv_flags
;
130 vmci_resource_put(resource
);
131 } else if (context_id
== VMCI_HYPERVISOR_CONTEXT_ID
)
132 *priv_flags
= VMCI_MAX_PRIVILEGE_FLAGS
;
134 *priv_flags
= vmci_context_get_priv_flags(context_id
);
140 * Calls the specified callback in a delayed context.
142 static void dg_delayed_dispatch(struct work_struct
*work
)
144 struct delayed_datagram_info
*dg_info
=
145 container_of(work
, struct delayed_datagram_info
, work
);
147 dg_info
->entry
->recv_cb(dg_info
->entry
->client_data
, &dg_info
->msg
);
149 vmci_resource_put(&dg_info
->entry
->resource
);
151 if (dg_info
->in_dg_host_queue
)
152 atomic_dec(&delayed_dg_host_queue_size
);
158 * Dispatch datagram as a host, to the host, or other vm context. This
159 * function cannot dispatch to hypervisor context handlers. This should
160 * have been handled before we get here by vmci_datagram_dispatch.
161 * Returns number of bytes sent on success, error code otherwise.
163 static int dg_dispatch_as_host(u32 context_id
, struct vmci_datagram
*dg
)
169 dg_size
= VMCI_DG_SIZE(dg
);
171 /* Host cannot send to the hypervisor. */
172 if (dg
->dst
.context
== VMCI_HYPERVISOR_CONTEXT_ID
)
173 return VMCI_ERROR_DST_UNREACHABLE
;
175 /* Check that source handle matches sending context. */
176 if (dg
->src
.context
!= context_id
) {
177 pr_devel("Sender context (ID=0x%x) is not owner of src datagram entry (handle=0x%x:0x%x)\n",
178 context_id
, dg
->src
.context
, dg
->src
.resource
);
179 return VMCI_ERROR_NO_ACCESS
;
182 /* Get hold of privileges of sending endpoint. */
183 retval
= vmci_datagram_get_priv_flags(context_id
, dg
->src
,
185 if (retval
!= VMCI_SUCCESS
) {
186 pr_warn("Couldn't get privileges (handle=0x%x:0x%x)\n",
187 dg
->src
.context
, dg
->src
.resource
);
191 /* Determine if we should route to host or guest destination. */
192 if (dg
->dst
.context
== VMCI_HOST_CONTEXT_ID
) {
193 /* Route to host datagram entry. */
194 struct datagram_entry
*dst_entry
;
195 struct vmci_resource
*resource
;
197 if (dg
->src
.context
== VMCI_HYPERVISOR_CONTEXT_ID
&&
198 dg
->dst
.resource
== VMCI_EVENT_HANDLER
) {
199 return vmci_event_dispatch(dg
);
202 resource
= vmci_resource_by_handle(dg
->dst
,
203 VMCI_RESOURCE_TYPE_DATAGRAM
);
205 pr_devel("Sending to invalid destination (handle=0x%x:0x%x)\n",
206 dg
->dst
.context
, dg
->dst
.resource
);
207 return VMCI_ERROR_INVALID_RESOURCE
;
209 dst_entry
= container_of(resource
, struct datagram_entry
,
211 if (vmci_deny_interaction(src_priv_flags
,
212 dst_entry
->priv_flags
)) {
213 vmci_resource_put(resource
);
214 return VMCI_ERROR_NO_ACCESS
;
218 * If a VMCI datagram destined for the host is also sent by the
219 * host, we always run it delayed. This ensures that no locks
220 * are held when the datagram callback runs.
222 if (dst_entry
->run_delayed
||
223 dg
->src
.context
== VMCI_HOST_CONTEXT_ID
) {
224 struct delayed_datagram_info
*dg_info
;
226 if (atomic_add_return(1, &delayed_dg_host_queue_size
)
227 == VMCI_MAX_DELAYED_DG_HOST_QUEUE_SIZE
) {
228 atomic_dec(&delayed_dg_host_queue_size
);
229 vmci_resource_put(resource
);
230 return VMCI_ERROR_NO_MEM
;
233 dg_info
= kmalloc(sizeof(*dg_info
) +
234 (size_t) dg
->payload_size
, GFP_ATOMIC
);
236 atomic_dec(&delayed_dg_host_queue_size
);
237 vmci_resource_put(resource
);
238 return VMCI_ERROR_NO_MEM
;
241 dg_info
->in_dg_host_queue
= true;
242 dg_info
->entry
= dst_entry
;
243 memcpy(&dg_info
->msg
, dg
, dg_size
);
245 INIT_WORK(&dg_info
->work
, dg_delayed_dispatch
);
246 schedule_work(&dg_info
->work
);
247 retval
= VMCI_SUCCESS
;
250 retval
= dst_entry
->recv_cb(dst_entry
->client_data
, dg
);
251 vmci_resource_put(resource
);
252 if (retval
< VMCI_SUCCESS
)
256 /* Route to destination VM context. */
257 struct vmci_datagram
*new_dg
;
259 if (context_id
!= dg
->dst
.context
) {
260 if (vmci_deny_interaction(src_priv_flags
,
261 vmci_context_get_priv_flags
262 (dg
->dst
.context
))) {
263 return VMCI_ERROR_NO_ACCESS
;
264 } else if (VMCI_CONTEXT_IS_VM(context_id
)) {
266 * If the sending context is a VM, it
267 * cannot reach another VM.
270 pr_devel("Datagram communication between VMs not supported (src=0x%x, dst=0x%x)\n",
271 context_id
, dg
->dst
.context
);
272 return VMCI_ERROR_DST_UNREACHABLE
;
276 /* We make a copy to enqueue. */
277 new_dg
= kmalloc(dg_size
, GFP_KERNEL
);
279 return VMCI_ERROR_NO_MEM
;
281 memcpy(new_dg
, dg
, dg_size
);
282 retval
= vmci_ctx_enqueue_datagram(dg
->dst
.context
, new_dg
);
283 if (retval
< VMCI_SUCCESS
) {
290 * We currently truncate the size to signed 32 bits. This doesn't
291 * matter for this handler as it only support 4Kb messages.
297 * Dispatch datagram as a guest, down through the VMX and potentially to
299 * Returns number of bytes sent on success, error code otherwise.
301 static int dg_dispatch_as_guest(struct vmci_datagram
*dg
)
304 struct vmci_resource
*resource
;
306 resource
= vmci_resource_by_handle(dg
->src
,
307 VMCI_RESOURCE_TYPE_DATAGRAM
);
309 return VMCI_ERROR_NO_HANDLE
;
311 retval
= vmci_send_datagram(dg
);
312 vmci_resource_put(resource
);
317 * Dispatch datagram. This will determine the routing for the datagram
318 * and dispatch it accordingly.
319 * Returns number of bytes sent on success, error code otherwise.
321 int vmci_datagram_dispatch(u32 context_id
,
322 struct vmci_datagram
*dg
, bool from_guest
)
325 enum vmci_route route
;
327 BUILD_BUG_ON(sizeof(struct vmci_datagram
) != 24);
329 if (VMCI_DG_SIZE(dg
) > VMCI_MAX_DG_SIZE
) {
330 pr_devel("Payload (size=%llu bytes) too big to send\n",
331 (unsigned long long)dg
->payload_size
);
332 return VMCI_ERROR_INVALID_ARGS
;
335 retval
= vmci_route(&dg
->src
, &dg
->dst
, from_guest
, &route
);
336 if (retval
< VMCI_SUCCESS
) {
337 pr_devel("Failed to route datagram (src=0x%x, dst=0x%x, err=%d)\n",
338 dg
->src
.context
, dg
->dst
.context
, retval
);
342 if (VMCI_ROUTE_AS_HOST
== route
) {
343 if (VMCI_INVALID_ID
== context_id
)
344 context_id
= VMCI_HOST_CONTEXT_ID
;
345 return dg_dispatch_as_host(context_id
, dg
);
348 if (VMCI_ROUTE_AS_GUEST
== route
)
349 return dg_dispatch_as_guest(dg
);
351 pr_warn("Unknown route (%d) for datagram\n", route
);
352 return VMCI_ERROR_DST_UNREACHABLE
;
356 * Invoke the handler for the given datagram. This is intended to be
357 * called only when acting as a guest and receiving a datagram from the
360 int vmci_datagram_invoke_guest_handler(struct vmci_datagram
*dg
)
362 struct vmci_resource
*resource
;
363 struct datagram_entry
*dst_entry
;
365 resource
= vmci_resource_by_handle(dg
->dst
,
366 VMCI_RESOURCE_TYPE_DATAGRAM
);
368 pr_devel("destination (handle=0x%x:0x%x) doesn't exist\n",
369 dg
->dst
.context
, dg
->dst
.resource
);
370 return VMCI_ERROR_NO_HANDLE
;
373 dst_entry
= container_of(resource
, struct datagram_entry
, resource
);
374 if (dst_entry
->run_delayed
) {
375 struct delayed_datagram_info
*dg_info
;
377 dg_info
= kmalloc(sizeof(*dg_info
) + (size_t)dg
->payload_size
,
380 vmci_resource_put(resource
);
381 return VMCI_ERROR_NO_MEM
;
384 dg_info
->in_dg_host_queue
= false;
385 dg_info
->entry
= dst_entry
;
386 memcpy(&dg_info
->msg
, dg
, VMCI_DG_SIZE(dg
));
388 INIT_WORK(&dg_info
->work
, dg_delayed_dispatch
);
389 schedule_work(&dg_info
->work
);
391 dst_entry
->recv_cb(dst_entry
->client_data
, dg
);
392 vmci_resource_put(resource
);
399 * vmci_datagram_create_handle_priv() - Create host context datagram endpoint
400 * @resource_id: The resource ID.
401 * @flags: Datagram Flags.
402 * @priv_flags: Privilege Flags.
403 * @recv_cb: Callback when receiving datagrams.
404 * @client_data: Pointer for a datagram_entry struct
405 * @out_handle: vmci_handle that is populated as a result of this function.
407 * Creates a host context datagram endpoint and returns a handle to it.
409 int vmci_datagram_create_handle_priv(u32 resource_id
,
412 vmci_datagram_recv_cb recv_cb
,
414 struct vmci_handle
*out_handle
)
416 if (out_handle
== NULL
)
417 return VMCI_ERROR_INVALID_ARGS
;
419 if (recv_cb
== NULL
) {
420 pr_devel("Client callback needed when creating datagram\n");
421 return VMCI_ERROR_INVALID_ARGS
;
424 if (priv_flags
& ~VMCI_PRIVILEGE_ALL_FLAGS
)
425 return VMCI_ERROR_INVALID_ARGS
;
427 return dg_create_handle(resource_id
, flags
, priv_flags
, recv_cb
,
428 client_data
, out_handle
);
430 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle_priv
);
433 * vmci_datagram_create_handle() - Create host context datagram endpoint
434 * @resource_id: Resource ID.
435 * @flags: Datagram Flags.
436 * @recv_cb: Callback when receiving datagrams.
437 * @client_ata: Pointer for a datagram_entry struct
438 * @out_handle: vmci_handle that is populated as a result of this function.
440 * Creates a host context datagram endpoint and returns a handle to
441 * it. Same as vmci_datagram_create_handle_priv without the priviledge
444 int vmci_datagram_create_handle(u32 resource_id
,
446 vmci_datagram_recv_cb recv_cb
,
448 struct vmci_handle
*out_handle
)
450 return vmci_datagram_create_handle_priv(
452 VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS
,
453 recv_cb
, client_data
,
456 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle
);
459 * vmci_datagram_destroy_handle() - Destroys datagram handle
460 * @handle: vmci_handle to be destroyed and reaped.
462 * Use this function to destroy any datagram handles created by
463 * vmci_datagram_create_handle{,Priv} functions.
465 int vmci_datagram_destroy_handle(struct vmci_handle handle
)
467 struct datagram_entry
*entry
;
468 struct vmci_resource
*resource
;
470 resource
= vmci_resource_by_handle(handle
, VMCI_RESOURCE_TYPE_DATAGRAM
);
472 pr_devel("Failed to destroy datagram (handle=0x%x:0x%x)\n",
473 handle
.context
, handle
.resource
);
474 return VMCI_ERROR_NOT_FOUND
;
477 entry
= container_of(resource
, struct datagram_entry
, resource
);
479 vmci_resource_put(&entry
->resource
);
480 vmci_resource_remove(&entry
->resource
);
485 EXPORT_SYMBOL_GPL(vmci_datagram_destroy_handle
);
488 * vmci_datagram_send() - Send a datagram
489 * @msg: The datagram to send.
491 * Sends the provided datagram on its merry way.
493 int vmci_datagram_send(struct vmci_datagram
*msg
)
496 return VMCI_ERROR_INVALID_ARGS
;
498 return vmci_datagram_dispatch(VMCI_INVALID_ID
, msg
, false);
500 EXPORT_SYMBOL_GPL(vmci_datagram_send
);