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;
237 memcpy(&dg_info->msg, dg, dg_size);
239 INIT_WORK(&dg_info->work, dg_delayed_dispatch);
240 schedule_work(&dg_info->work);
241 retval = VMCI_SUCCESS;
244 retval = dst_entry->recv_cb(dst_entry->client_data, dg);
245 vmci_resource_put(resource);
246 if (retval < VMCI_SUCCESS)
250 /* Route to destination VM context. */
251 struct vmci_datagram *new_dg;
253 if (context_id != dg->dst.context) {
254 if (vmci_deny_interaction(src_priv_flags,
255 vmci_context_get_priv_flags
256 (dg->dst.context))) {
257 return VMCI_ERROR_NO_ACCESS;
258 } else if (VMCI_CONTEXT_IS_VM(context_id)) {
260 * If the sending context is a VM, it
261 * cannot reach another VM.
264 pr_devel("Datagram communication between VMs not supported (src=0x%x, dst=0x%x)\n",
265 context_id, dg->dst.context);
266 return VMCI_ERROR_DST_UNREACHABLE;
270 /* We make a copy to enqueue. */
271 new_dg = kmemdup(dg, dg_size, GFP_KERNEL);
273 return VMCI_ERROR_NO_MEM;
275 retval = vmci_ctx_enqueue_datagram(dg->dst.context, new_dg);
276 if (retval < VMCI_SUCCESS) {
283 * We currently truncate the size to signed 32 bits. This doesn't
284 * matter for this handler as it only support 4Kb messages.
290 * Dispatch datagram as a guest, down through the VMX and potentially to
292 * Returns number of bytes sent on success, error code otherwise.
294 static int dg_dispatch_as_guest(struct vmci_datagram *dg)
297 struct vmci_resource *resource;
299 resource = vmci_resource_by_handle(dg->src,
300 VMCI_RESOURCE_TYPE_DATAGRAM);
302 return VMCI_ERROR_NO_HANDLE;
304 retval = vmci_send_datagram(dg);
305 vmci_resource_put(resource);
310 * Dispatch datagram. This will determine the routing for the datagram
311 * and dispatch it accordingly.
312 * Returns number of bytes sent on success, error code otherwise.
314 int vmci_datagram_dispatch(u32 context_id,
315 struct vmci_datagram *dg, bool from_guest)
318 enum vmci_route route;
320 BUILD_BUG_ON(sizeof(struct vmci_datagram) != 24);
322 if (dg->payload_size > VMCI_MAX_DG_SIZE ||
323 VMCI_DG_SIZE(dg) > VMCI_MAX_DG_SIZE) {
324 pr_devel("Payload (size=%llu bytes) too big to send\n",
325 (unsigned long long)dg->payload_size);
326 return VMCI_ERROR_INVALID_ARGS;
329 retval = vmci_route(&dg->src, &dg->dst, from_guest, &route);
330 if (retval < VMCI_SUCCESS) {
331 pr_devel("Failed to route datagram (src=0x%x, dst=0x%x, err=%d)\n",
332 dg->src.context, dg->dst.context, retval);
336 if (VMCI_ROUTE_AS_HOST == route) {
337 if (VMCI_INVALID_ID == context_id)
338 context_id = VMCI_HOST_CONTEXT_ID;
339 return dg_dispatch_as_host(context_id, dg);
342 if (VMCI_ROUTE_AS_GUEST == route)
343 return dg_dispatch_as_guest(dg);
345 pr_warn("Unknown route (%d) for datagram\n", route);
346 return VMCI_ERROR_DST_UNREACHABLE;
350 * Invoke the handler for the given datagram. This is intended to be
351 * called only when acting as a guest and receiving a datagram from the
354 int vmci_datagram_invoke_guest_handler(struct vmci_datagram *dg)
356 struct vmci_resource *resource;
357 struct datagram_entry *dst_entry;
359 resource = vmci_resource_by_handle(dg->dst,
360 VMCI_RESOURCE_TYPE_DATAGRAM);
362 pr_devel("destination (handle=0x%x:0x%x) doesn't exist\n",
363 dg->dst.context, dg->dst.resource);
364 return VMCI_ERROR_NO_HANDLE;
367 dst_entry = container_of(resource, struct datagram_entry, resource);
368 if (dst_entry->run_delayed) {
369 struct delayed_datagram_info *dg_info;
371 dg_info = kmalloc(sizeof(*dg_info) + (size_t)dg->payload_size,
374 vmci_resource_put(resource);
375 return VMCI_ERROR_NO_MEM;
378 dg_info->in_dg_host_queue = false;
379 dg_info->entry = dst_entry;
380 memcpy(&dg_info->msg, dg, VMCI_DG_SIZE(dg));
382 INIT_WORK(&dg_info->work, dg_delayed_dispatch);
383 schedule_work(&dg_info->work);
385 dst_entry->recv_cb(dst_entry->client_data, dg);
386 vmci_resource_put(resource);
393 * vmci_datagram_create_handle_priv() - Create host context datagram endpoint
394 * @resource_id: The resource ID.
395 * @flags: Datagram Flags.
396 * @priv_flags: Privilege Flags.
397 * @recv_cb: Callback when receiving datagrams.
398 * @client_data: Pointer for a datagram_entry struct
399 * @out_handle: vmci_handle that is populated as a result of this function.
401 * Creates a host context datagram endpoint and returns a handle to it.
403 int vmci_datagram_create_handle_priv(u32 resource_id,
406 vmci_datagram_recv_cb recv_cb,
408 struct vmci_handle *out_handle)
410 if (out_handle == NULL)
411 return VMCI_ERROR_INVALID_ARGS;
413 if (recv_cb == NULL) {
414 pr_devel("Client callback needed when creating datagram\n");
415 return VMCI_ERROR_INVALID_ARGS;
418 if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS)
419 return VMCI_ERROR_INVALID_ARGS;
421 return dg_create_handle(resource_id, flags, priv_flags, recv_cb,
422 client_data, out_handle);
424 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle_priv);
427 * vmci_datagram_create_handle() - Create host context datagram endpoint
428 * @resource_id: Resource ID.
429 * @flags: Datagram Flags.
430 * @recv_cb: Callback when receiving datagrams.
431 * @client_ata: Pointer for a datagram_entry struct
432 * @out_handle: vmci_handle that is populated as a result of this function.
434 * Creates a host context datagram endpoint and returns a handle to
435 * it. Same as vmci_datagram_create_handle_priv without the priviledge
438 int vmci_datagram_create_handle(u32 resource_id,
440 vmci_datagram_recv_cb recv_cb,
442 struct vmci_handle *out_handle)
444 return vmci_datagram_create_handle_priv(
446 VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
447 recv_cb, client_data,
450 EXPORT_SYMBOL_GPL(vmci_datagram_create_handle);
453 * vmci_datagram_destroy_handle() - Destroys datagram handle
454 * @handle: vmci_handle to be destroyed and reaped.
456 * Use this function to destroy any datagram handles created by
457 * vmci_datagram_create_handle{,Priv} functions.
459 int vmci_datagram_destroy_handle(struct vmci_handle handle)
461 struct datagram_entry *entry;
462 struct vmci_resource *resource;
464 resource = vmci_resource_by_handle(handle, VMCI_RESOURCE_TYPE_DATAGRAM);
466 pr_devel("Failed to destroy datagram (handle=0x%x:0x%x)\n",
467 handle.context, handle.resource);
468 return VMCI_ERROR_NOT_FOUND;
471 entry = container_of(resource, struct datagram_entry, resource);
473 vmci_resource_put(&entry->resource);
474 vmci_resource_remove(&entry->resource);
479 EXPORT_SYMBOL_GPL(vmci_datagram_destroy_handle);
482 * vmci_datagram_send() - Send a datagram
483 * @msg: The datagram to send.
485 * Sends the provided datagram on its merry way.
487 int vmci_datagram_send(struct vmci_datagram *msg)
490 return VMCI_ERROR_INVALID_ARGS;
492 return vmci_datagram_dispatch(VMCI_INVALID_ID, msg, false);
494 EXPORT_SYMBOL_GPL(vmci_datagram_send);