1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/vmw_vmci_api.h>
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/bug.h>
15 #include "vmci_datagram.h"
16 #include "vmci_resource.h"
17 #include "vmci_context.h"
18 #include "vmci_driver.h"
19 #include "vmci_event.h"
20 #include "vmci_route.h"
23 * struct datagram_entry describes the datagram entity. It is used for datagram
24 * entities created only on the host.
26 struct datagram_entry {
27 struct vmci_resource resource;
30 vmci_datagram_recv_cb recv_cb;
35 struct delayed_datagram_info {
36 struct datagram_entry *entry;
37 struct work_struct work;
38 bool in_dg_host_queue;
39 /* msg and msg_payload must be together. */
40 struct vmci_datagram msg;
44 /* Number of in-flight host->host datagrams */
45 static atomic_t delayed_dg_host_queue_size = ATOMIC_INIT(0);
48 * Create a datagram entry given a handle pointer.
50 static int dg_create_handle(u32 resource_id,
53 vmci_datagram_recv_cb recv_cb,
54 void *client_data, struct vmci_handle *out_handle)
58 struct vmci_handle handle;
59 struct datagram_entry *entry;
61 if ((flags & VMCI_FLAG_WELLKNOWN_DG_HND) != 0)
62 return VMCI_ERROR_INVALID_ARGS;
64 if ((flags & VMCI_FLAG_ANYCID_DG_HND) != 0) {
65 context_id = VMCI_INVALID_ID;
67 context_id = vmci_get_context_id();
68 if (context_id == VMCI_INVALID_ID)
69 return VMCI_ERROR_NO_RESOURCES;
72 handle = vmci_make_handle(context_id, resource_id);
74 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
76 pr_warn("Failed allocating memory for datagram entry\n");
77 return VMCI_ERROR_NO_MEM;
80 entry->run_delayed = (flags & VMCI_FLAG_DG_DELAYED_CB) ? true : false;
82 entry->recv_cb = recv_cb;
83 entry->client_data = client_data;
84 entry->priv_flags = priv_flags;
86 /* Make datagram resource live. */
87 result = vmci_resource_add(&entry->resource,
88 VMCI_RESOURCE_TYPE_DATAGRAM,
90 if (result != VMCI_SUCCESS) {
91 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d\n",
92 handle.context, handle.resource, result);
97 *out_handle = vmci_resource_handle(&entry->resource);
102 * Internal utility function with the same purpose as
103 * vmci_datagram_get_priv_flags that also takes a context_id.
105 static int vmci_datagram_get_priv_flags(u32 context_id,
106 struct vmci_handle handle,
109 if (context_id == VMCI_INVALID_ID)
110 return VMCI_ERROR_INVALID_ARGS;
112 if (context_id == VMCI_HOST_CONTEXT_ID) {
113 struct datagram_entry *src_entry;
114 struct vmci_resource *resource;
116 resource = vmci_resource_by_handle(handle,
117 VMCI_RESOURCE_TYPE_DATAGRAM);
119 return VMCI_ERROR_INVALID_ARGS;
121 src_entry = container_of(resource, struct datagram_entry,
123 *priv_flags = src_entry->priv_flags;
124 vmci_resource_put(resource);
125 } else if (context_id == VMCI_HYPERVISOR_CONTEXT_ID)
126 *priv_flags = VMCI_MAX_PRIVILEGE_FLAGS;
128 *priv_flags = vmci_context_get_priv_flags(context_id);
134 * Calls the specified callback in a delayed context.
136 static void dg_delayed_dispatch(struct work_struct *work)
138 struct delayed_datagram_info *dg_info =
139 container_of(work, struct delayed_datagram_info, work);
141 dg_info->entry->recv_cb(dg_info->entry->client_data, &dg_info->msg);
143 vmci_resource_put(&dg_info->entry->resource);
145 if (dg_info->in_dg_host_queue)
146 atomic_dec(&delayed_dg_host_queue_size);
152 * Dispatch datagram as a host, to the host, or other vm context. This
153 * function cannot dispatch to hypervisor context handlers. This should
154 * have been handled before we get here by vmci_datagram_dispatch.
155 * Returns number of bytes sent on success, error code otherwise.
157 static int dg_dispatch_as_host(u32 context_id, struct vmci_datagram *dg)
163 dg_size = VMCI_DG_SIZE(dg);
165 /* Host cannot send to the hypervisor. */
166 if (dg->dst.context == VMCI_HYPERVISOR_CONTEXT_ID)
167 return VMCI_ERROR_DST_UNREACHABLE;
169 /* Check that source handle matches sending context. */
170 if (dg->src.context != context_id) {
171 pr_devel("Sender context (ID=0x%x) is not owner of src datagram entry (handle=0x%x:0x%x)\n",
172 context_id, dg->src.context, dg->src.resource);
173 return VMCI_ERROR_NO_ACCESS;
176 /* Get hold of privileges of sending endpoint. */
177 retval = vmci_datagram_get_priv_flags(context_id, dg->src,
179 if (retval != VMCI_SUCCESS) {
180 pr_warn("Couldn't get privileges (handle=0x%x:0x%x)\n",
181 dg->src.context, dg->src.resource);
185 /* Determine if we should route to host or guest destination. */
186 if (dg->dst.context == VMCI_HOST_CONTEXT_ID) {
187 /* Route to host datagram entry. */
188 struct datagram_entry *dst_entry;
189 struct vmci_resource *resource;
191 if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
192 dg->dst.resource == VMCI_EVENT_HANDLER) {
193 return vmci_event_dispatch(dg);
196 resource = vmci_resource_by_handle(dg->dst,
197 VMCI_RESOURCE_TYPE_DATAGRAM);
199 pr_devel("Sending to invalid destination (handle=0x%x:0x%x)\n",
200 dg->dst.context, dg->dst.resource);
201 return VMCI_ERROR_INVALID_RESOURCE;
203 dst_entry = container_of(resource, struct datagram_entry,
205 if (vmci_deny_interaction(src_priv_flags,
206 dst_entry->priv_flags)) {
207 vmci_resource_put(resource);
208 return VMCI_ERROR_NO_ACCESS;
212 * If a VMCI datagram destined for the host is also sent by the
213 * host, we always run it delayed. This ensures that no locks
214 * are held when the datagram callback runs.
216 if (dst_entry->run_delayed ||
217 dg->src.context == VMCI_HOST_CONTEXT_ID) {
218 struct delayed_datagram_info *dg_info;
220 if (atomic_add_return(1, &delayed_dg_host_queue_size)
221 == VMCI_MAX_DELAYED_DG_HOST_QUEUE_SIZE) {
222 atomic_dec(&delayed_dg_host_queue_size);
223 vmci_resource_put(resource);
224 return VMCI_ERROR_NO_MEM;
227 dg_info = kmalloc(sizeof(*dg_info) +
228 (size_t) dg->payload_size, GFP_ATOMIC);
230 atomic_dec(&delayed_dg_host_queue_size);
231 vmci_resource_put(resource);
232 return VMCI_ERROR_NO_MEM;
235 dg_info->in_dg_host_queue = true;
236 dg_info->entry = dst_entry;
238 memcpy(&dg_info->msg_payload, dg + 1, dg->payload_size);
240 INIT_WORK(&dg_info->work, dg_delayed_dispatch);
241 schedule_work(&dg_info->work);
242 retval = VMCI_SUCCESS;
245 retval = dst_entry->recv_cb(dst_entry->client_data, dg);
246 vmci_resource_put(resource);
247 if (retval < VMCI_SUCCESS)
251 /* Route to destination VM context. */
252 struct vmci_datagram *new_dg;
254 if (context_id != dg->dst.context) {
255 if (vmci_deny_interaction(src_priv_flags,
256 vmci_context_get_priv_flags
257 (dg->dst.context))) {
258 return VMCI_ERROR_NO_ACCESS;
259 } else if (VMCI_CONTEXT_IS_VM(context_id)) {
261 * If the sending context is a VM, it
262 * cannot reach another VM.
265 pr_devel("Datagram communication between VMs not supported (src=0x%x, dst=0x%x)\n",
266 context_id, dg->dst.context);
267 return VMCI_ERROR_DST_UNREACHABLE;
271 /* We make a copy to enqueue. */
272 new_dg = kmemdup(dg, dg_size, GFP_KERNEL);
274 return VMCI_ERROR_NO_MEM;
276 retval = vmci_ctx_enqueue_datagram(dg->dst.context, new_dg);
277 if (retval < VMCI_SUCCESS) {
284 * We currently truncate the size to signed 32 bits. This doesn't
285 * matter for this handler as it only support 4Kb messages.
291 * Dispatch datagram as a guest, down through the VMX and potentially to
293 * Returns number of bytes sent on success, error code otherwise.
295 static int dg_dispatch_as_guest(struct vmci_datagram *dg)
298 struct vmci_resource *resource;
300 resource = vmci_resource_by_handle(dg->src,
301 VMCI_RESOURCE_TYPE_DATAGRAM);
303 return VMCI_ERROR_NO_HANDLE;
305 retval = vmci_send_datagram(dg);
306 vmci_resource_put(resource);
311 * Dispatch datagram. This will determine the routing for the datagram
312 * and dispatch it accordingly.
313 * Returns number of bytes sent on success, error code otherwise.
315 int vmci_datagram_dispatch(u32 context_id,
316 struct vmci_datagram *dg, bool from_guest)
319 enum vmci_route route;
321 BUILD_BUG_ON(sizeof(struct vmci_datagram) != 24);
323 if (dg->payload_size > VMCI_MAX_DG_SIZE ||
324 VMCI_DG_SIZE(dg) > VMCI_MAX_DG_SIZE) {
325 pr_devel("Payload (size=%llu bytes) too big to send\n",
326 (unsigned long long)dg->payload_size);
327 return VMCI_ERROR_INVALID_ARGS;
330 retval = vmci_route(&dg->src, &dg->dst, from_guest, &route);
331 if (retval < VMCI_SUCCESS) {
332 pr_devel("Failed to route datagram (src=0x%x, dst=0x%x, err=%d)\n",
333 dg->src.context, dg->dst.context, retval);
337 if (VMCI_ROUTE_AS_HOST == route) {
338 if (VMCI_INVALID_ID == context_id)
339 context_id = VMCI_HOST_CONTEXT_ID;
340 return dg_dispatch_as_host(context_id, dg);
343 if (VMCI_ROUTE_AS_GUEST == route)
344 return dg_dispatch_as_guest(dg);
346 pr_warn("Unknown route (%d) for datagram\n", route);
347 return VMCI_ERROR_DST_UNREACHABLE;
351 * Invoke the handler for the given datagram. This is intended to be
352 * called only when acting as a guest and receiving a datagram from the
355 int vmci_datagram_invoke_guest_handler(struct vmci_datagram *dg)
357 struct vmci_resource *resource;
358 struct datagram_entry *dst_entry;
360 resource = vmci_resource_by_handle(dg->dst,
361 VMCI_RESOURCE_TYPE_DATAGRAM);
363 pr_devel("destination (handle=0x%x:0x%x) doesn't exist\n",
364 dg->dst.context, dg->dst.resource);
365 return VMCI_ERROR_NO_HANDLE;
368 dst_entry = container_of(resource, struct datagram_entry, resource);
369 if (dst_entry->run_delayed) {
370 struct delayed_datagram_info *dg_info;
372 dg_info = kmalloc(sizeof(*dg_info) + (size_t)dg->payload_size,
375 vmci_resource_put(resource);
376 return VMCI_ERROR_NO_MEM;
379 dg_info->in_dg_host_queue = false;
380 dg_info->entry = dst_entry;
382 memcpy(&dg_info->msg_payload, dg + 1, dg->payload_size);
384 INIT_WORK(&dg_info->work, dg_delayed_dispatch);
385 schedule_work(&dg_info->work);
387 dst_entry->recv_cb(dst_entry->client_data, dg);
388 vmci_resource_put(resource);
395 * vmci_datagram_create_handle_priv() - Create host context datagram endpoint
396 * @resource_id: The resource ID.
397 * @flags: Datagram Flags.
398 * @priv_flags: Privilege Flags.
399 * @recv_cb: Callback when receiving datagrams.
400 * @client_data: Pointer for a datagram_entry struct
401 * @out_handle: vmci_handle that is populated as a result of this function.
403 * Creates a host context datagram endpoint and returns a handle to it.
405 int vmci_datagram_create_handle_priv(u32 resource_id,
408 vmci_datagram_recv_cb recv_cb,
410 struct vmci_handle *out_handle)
412 if (out_handle == NULL)
413 return VMCI_ERROR_INVALID_ARGS;
415 if (recv_cb == NULL) {
416 pr_devel("Client callback needed when creating datagram\n");
417 return VMCI_ERROR_INVALID_ARGS;
420 if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS)
421 return VMCI_ERROR_INVALID_ARGS;
423 return dg_create_handle(resource_id, flags, priv_flags, recv_cb,
424 client_data, out_handle);
426 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle_priv);
429 * vmci_datagram_create_handle() - Create host context datagram endpoint
430 * @resource_id: Resource ID.
431 * @flags: Datagram Flags.
432 * @recv_cb: Callback when receiving datagrams.
433 * @client_ata: Pointer for a datagram_entry struct
434 * @out_handle: vmci_handle that is populated as a result of this function.
436 * Creates a host context datagram endpoint and returns a handle to
437 * it. Same as vmci_datagram_create_handle_priv without the priviledge
440 int vmci_datagram_create_handle(u32 resource_id,
442 vmci_datagram_recv_cb recv_cb,
444 struct vmci_handle *out_handle)
446 return vmci_datagram_create_handle_priv(
448 VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
449 recv_cb, client_data,
452 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle);
455 * vmci_datagram_destroy_handle() - Destroys datagram handle
456 * @handle: vmci_handle to be destroyed and reaped.
458 * Use this function to destroy any datagram handles created by
459 * vmci_datagram_create_handle{,Priv} functions.
461 int vmci_datagram_destroy_handle(struct vmci_handle handle)
463 struct datagram_entry *entry;
464 struct vmci_resource *resource;
466 resource = vmci_resource_by_handle(handle, VMCI_RESOURCE_TYPE_DATAGRAM);
468 pr_devel("Failed to destroy datagram (handle=0x%x:0x%x)\n",
469 handle.context, handle.resource);
470 return VMCI_ERROR_NOT_FOUND;
473 entry = container_of(resource, struct datagram_entry, resource);
475 vmci_resource_put(&entry->resource);
476 vmci_resource_remove(&entry->resource);
481 EXPORT_SYMBOL_GPL(vmci_datagram_destroy_handle);
484 * vmci_datagram_send() - Send a datagram
485 * @msg: The datagram to send.
487 * Sends the provided datagram on its merry way.
489 int vmci_datagram_send(struct vmci_datagram *msg)
492 return VMCI_ERROR_INVALID_ARGS;
494 return vmci_datagram_dispatch(VMCI_INVALID_ID, msg, false);
496 EXPORT_SYMBOL_GPL(vmci_datagram_send);