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 (dg->payload_size > VMCI_MAX_DG_SIZE ||
332 VMCI_DG_SIZE(dg) > VMCI_MAX_DG_SIZE) {
333 pr_devel("Payload (size=%llu bytes) too big to send\n",
334 (unsigned long long)dg->payload_size);
335 return VMCI_ERROR_INVALID_ARGS;
338 retval = vmci_route(&dg->src, &dg->dst, from_guest, &route);
339 if (retval < VMCI_SUCCESS) {
340 pr_devel("Failed to route datagram (src=0x%x, dst=0x%x, err=%d)\n",
341 dg->src.context, dg->dst.context, retval);
345 if (VMCI_ROUTE_AS_HOST == route) {
346 if (VMCI_INVALID_ID == context_id)
347 context_id = VMCI_HOST_CONTEXT_ID;
348 return dg_dispatch_as_host(context_id, dg);
351 if (VMCI_ROUTE_AS_GUEST == route)
352 return dg_dispatch_as_guest(dg);
354 pr_warn("Unknown route (%d) for datagram\n", route);
355 return VMCI_ERROR_DST_UNREACHABLE;
359 * Invoke the handler for the given datagram. This is intended to be
360 * called only when acting as a guest and receiving a datagram from the
363 int vmci_datagram_invoke_guest_handler(struct vmci_datagram *dg)
365 struct vmci_resource *resource;
366 struct datagram_entry *dst_entry;
368 resource = vmci_resource_by_handle(dg->dst,
369 VMCI_RESOURCE_TYPE_DATAGRAM);
371 pr_devel("destination (handle=0x%x:0x%x) doesn't exist\n",
372 dg->dst.context, dg->dst.resource);
373 return VMCI_ERROR_NO_HANDLE;
376 dst_entry = container_of(resource, struct datagram_entry, resource);
377 if (dst_entry->run_delayed) {
378 struct delayed_datagram_info *dg_info;
380 dg_info = kmalloc(sizeof(*dg_info) + (size_t)dg->payload_size,
383 vmci_resource_put(resource);
384 return VMCI_ERROR_NO_MEM;
387 dg_info->in_dg_host_queue = false;
388 dg_info->entry = dst_entry;
389 memcpy(&dg_info->msg, dg, VMCI_DG_SIZE(dg));
391 INIT_WORK(&dg_info->work, dg_delayed_dispatch);
392 schedule_work(&dg_info->work);
394 dst_entry->recv_cb(dst_entry->client_data, dg);
395 vmci_resource_put(resource);
402 * vmci_datagram_create_handle_priv() - Create host context datagram endpoint
403 * @resource_id: The resource ID.
404 * @flags: Datagram Flags.
405 * @priv_flags: Privilege Flags.
406 * @recv_cb: Callback when receiving datagrams.
407 * @client_data: Pointer for a datagram_entry struct
408 * @out_handle: vmci_handle that is populated as a result of this function.
410 * Creates a host context datagram endpoint and returns a handle to it.
412 int vmci_datagram_create_handle_priv(u32 resource_id,
415 vmci_datagram_recv_cb recv_cb,
417 struct vmci_handle *out_handle)
419 if (out_handle == NULL)
420 return VMCI_ERROR_INVALID_ARGS;
422 if (recv_cb == NULL) {
423 pr_devel("Client callback needed when creating datagram\n");
424 return VMCI_ERROR_INVALID_ARGS;
427 if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS)
428 return VMCI_ERROR_INVALID_ARGS;
430 return dg_create_handle(resource_id, flags, priv_flags, recv_cb,
431 client_data, out_handle);
433 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle_priv);
436 * vmci_datagram_create_handle() - Create host context datagram endpoint
437 * @resource_id: Resource ID.
438 * @flags: Datagram Flags.
439 * @recv_cb: Callback when receiving datagrams.
440 * @client_ata: Pointer for a datagram_entry struct
441 * @out_handle: vmci_handle that is populated as a result of this function.
443 * Creates a host context datagram endpoint and returns a handle to
444 * it. Same as vmci_datagram_create_handle_priv without the priviledge
447 int vmci_datagram_create_handle(u32 resource_id,
449 vmci_datagram_recv_cb recv_cb,
451 struct vmci_handle *out_handle)
453 return vmci_datagram_create_handle_priv(
455 VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
456 recv_cb, client_data,
459 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle);
462 * vmci_datagram_destroy_handle() - Destroys datagram handle
463 * @handle: vmci_handle to be destroyed and reaped.
465 * Use this function to destroy any datagram handles created by
466 * vmci_datagram_create_handle{,Priv} functions.
468 int vmci_datagram_destroy_handle(struct vmci_handle handle)
470 struct datagram_entry *entry;
471 struct vmci_resource *resource;
473 resource = vmci_resource_by_handle(handle, VMCI_RESOURCE_TYPE_DATAGRAM);
475 pr_devel("Failed to destroy datagram (handle=0x%x:0x%x)\n",
476 handle.context, handle.resource);
477 return VMCI_ERROR_NOT_FOUND;
480 entry = container_of(resource, struct datagram_entry, resource);
482 vmci_resource_put(&entry->resource);
483 vmci_resource_remove(&entry->resource);
488 EXPORT_SYMBOL_GPL(vmci_datagram_destroy_handle);
491 * vmci_datagram_send() - Send a datagram
492 * @msg: The datagram to send.
494 * Sends the provided datagram on its merry way.
496 int vmci_datagram_send(struct vmci_datagram *msg)
499 return VMCI_ERROR_INVALID_ARGS;
501 return vmci_datagram_dispatch(VMCI_INVALID_ID, msg, false);
503 EXPORT_SYMBOL_GPL(vmci_datagram_send);