Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

VMCI: datagram implementation.

VMCI datagram Implements datagrams to allow data to be sent between host and guest.

Signed-off-by: George Zhang <georgezhang@vmware.com>
Acked-by: Andy king <acking@vmware.com>
Acked-by: Dmitry Torokhov <dtor@vmware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

George Zhang and committed by
Greg Kroah-Hartman
a110b7eb 28d6692c

+552
+500
drivers/misc/vmw_vmci/vmci_datagram.c
··· 1 + /* 2 + * VMware VMCI Driver 3 + * 4 + * Copyright (C) 2012 VMware, Inc. All rights reserved. 5 + * 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. 9 + * 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 13 + * for more details. 14 + */ 15 + 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> 22 + 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" 29 + 30 + /* 31 + * struct datagram_entry describes the datagram entity. It is used for datagram 32 + * entities created only on the host. 33 + */ 34 + struct datagram_entry { 35 + struct vmci_resource resource; 36 + u32 flags; 37 + bool run_delayed; 38 + vmci_datagram_recv_cb recv_cb; 39 + void *client_data; 40 + u32 priv_flags; 41 + }; 42 + 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; 48 + }; 49 + 50 + /* Number of in-flight host->host datagrams */ 51 + static atomic_t delayed_dg_host_queue_size = ATOMIC_INIT(0); 52 + 53 + /* 54 + * Create a datagram entry given a handle pointer. 55 + */ 56 + static int dg_create_handle(u32 resource_id, 57 + u32 flags, 58 + u32 priv_flags, 59 + vmci_datagram_recv_cb recv_cb, 60 + void *client_data, struct vmci_handle *out_handle) 61 + { 62 + int result; 63 + u32 context_id; 64 + struct vmci_handle handle; 65 + struct datagram_entry *entry; 66 + 67 + if ((flags & VMCI_FLAG_WELLKNOWN_DG_HND) != 0) 68 + return VMCI_ERROR_INVALID_ARGS; 69 + 70 + if ((flags & VMCI_FLAG_ANYCID_DG_HND) != 0) { 71 + context_id = VMCI_INVALID_ID; 72 + } else { 73 + context_id = vmci_get_context_id(); 74 + if (context_id == VMCI_INVALID_ID) 75 + return VMCI_ERROR_NO_RESOURCES; 76 + } 77 + 78 + handle = vmci_make_handle(context_id, resource_id); 79 + 80 + entry = kmalloc(sizeof(*entry), GFP_KERNEL); 81 + if (!entry) { 82 + pr_warn("Failed allocating memory for datagram entry\n"); 83 + return VMCI_ERROR_NO_MEM; 84 + } 85 + 86 + entry->run_delayed = (flags & VMCI_FLAG_DG_DELAYED_CB) ? true : false; 87 + entry->flags = flags; 88 + entry->recv_cb = recv_cb; 89 + entry->client_data = client_data; 90 + entry->priv_flags = priv_flags; 91 + 92 + /* Make datagram resource live. */ 93 + result = vmci_resource_add(&entry->resource, 94 + VMCI_RESOURCE_TYPE_DATAGRAM, 95 + handle); 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); 99 + kfree(entry); 100 + return result; 101 + } 102 + 103 + *out_handle = vmci_resource_handle(&entry->resource); 104 + return VMCI_SUCCESS; 105 + } 106 + 107 + /* 108 + * Internal utility function with the same purpose as 109 + * vmci_datagram_get_priv_flags that also takes a context_id. 110 + */ 111 + static int vmci_datagram_get_priv_flags(u32 context_id, 112 + struct vmci_handle handle, 113 + u32 *priv_flags) 114 + { 115 + if (context_id == VMCI_INVALID_ID) 116 + return VMCI_ERROR_INVALID_ARGS; 117 + 118 + if (context_id == VMCI_HOST_CONTEXT_ID) { 119 + struct datagram_entry *src_entry; 120 + struct vmci_resource *resource; 121 + 122 + resource = vmci_resource_by_handle(handle, 123 + VMCI_RESOURCE_TYPE_DATAGRAM); 124 + if (!resource) 125 + return VMCI_ERROR_INVALID_ARGS; 126 + 127 + src_entry = container_of(resource, struct datagram_entry, 128 + resource); 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; 133 + else 134 + *priv_flags = vmci_context_get_priv_flags(context_id); 135 + 136 + return VMCI_SUCCESS; 137 + } 138 + 139 + /* 140 + * Calls the specified callback in a delayed context. 141 + */ 142 + static void dg_delayed_dispatch(struct work_struct *work) 143 + { 144 + struct delayed_datagram_info *dg_info = 145 + container_of(work, struct delayed_datagram_info, work); 146 + 147 + dg_info->entry->recv_cb(dg_info->entry->client_data, &dg_info->msg); 148 + 149 + vmci_resource_put(&dg_info->entry->resource); 150 + 151 + if (dg_info->in_dg_host_queue) 152 + atomic_dec(&delayed_dg_host_queue_size); 153 + 154 + kfree(dg_info); 155 + } 156 + 157 + /* 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. 162 + */ 163 + static int dg_dispatch_as_host(u32 context_id, struct vmci_datagram *dg) 164 + { 165 + int retval; 166 + size_t dg_size; 167 + u32 src_priv_flags; 168 + 169 + dg_size = VMCI_DG_SIZE(dg); 170 + 171 + /* Host cannot send to the hypervisor. */ 172 + if (dg->dst.context == VMCI_HYPERVISOR_CONTEXT_ID) 173 + return VMCI_ERROR_DST_UNREACHABLE; 174 + 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; 180 + } 181 + 182 + /* Get hold of privileges of sending endpoint. */ 183 + retval = vmci_datagram_get_priv_flags(context_id, dg->src, 184 + &src_priv_flags); 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); 188 + return retval; 189 + } 190 + 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; 196 + 197 + if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID && 198 + dg->dst.resource == VMCI_EVENT_HANDLER) { 199 + return vmci_event_dispatch(dg); 200 + } 201 + 202 + resource = vmci_resource_by_handle(dg->dst, 203 + VMCI_RESOURCE_TYPE_DATAGRAM); 204 + if (!resource) { 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; 208 + } 209 + dst_entry = container_of(resource, struct datagram_entry, 210 + resource); 211 + if (vmci_deny_interaction(src_priv_flags, 212 + dst_entry->priv_flags)) { 213 + vmci_resource_put(resource); 214 + return VMCI_ERROR_NO_ACCESS; 215 + } 216 + 217 + /* 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. 221 + */ 222 + if (dst_entry->run_delayed || 223 + dg->src.context == VMCI_HOST_CONTEXT_ID) { 224 + struct delayed_datagram_info *dg_info; 225 + 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; 231 + } 232 + 233 + dg_info = kmalloc(sizeof(*dg_info) + 234 + (size_t) dg->payload_size, GFP_ATOMIC); 235 + if (!dg_info) { 236 + atomic_dec(&delayed_dg_host_queue_size); 237 + vmci_resource_put(resource); 238 + return VMCI_ERROR_NO_MEM; 239 + } 240 + 241 + dg_info->in_dg_host_queue = true; 242 + dg_info->entry = dst_entry; 243 + memcpy(&dg_info->msg, dg, dg_size); 244 + 245 + INIT_WORK(&dg_info->work, dg_delayed_dispatch); 246 + schedule_work(&dg_info->work); 247 + retval = VMCI_SUCCESS; 248 + 249 + } else { 250 + retval = dst_entry->recv_cb(dst_entry->client_data, dg); 251 + vmci_resource_put(resource); 252 + if (retval < VMCI_SUCCESS) 253 + return retval; 254 + } 255 + } else { 256 + /* Route to destination VM context. */ 257 + struct vmci_datagram *new_dg; 258 + 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)) { 265 + /* 266 + * If the sending context is a VM, it 267 + * cannot reach another VM. 268 + */ 269 + 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; 273 + } 274 + } 275 + 276 + /* We make a copy to enqueue. */ 277 + new_dg = kmalloc(dg_size, GFP_KERNEL); 278 + if (new_dg == NULL) 279 + return VMCI_ERROR_NO_MEM; 280 + 281 + memcpy(new_dg, dg, dg_size); 282 + retval = vmci_ctx_enqueue_datagram(dg->dst.context, new_dg); 283 + if (retval < VMCI_SUCCESS) { 284 + kfree(new_dg); 285 + return retval; 286 + } 287 + } 288 + 289 + /* 290 + * We currently truncate the size to signed 32 bits. This doesn't 291 + * matter for this handler as it only support 4Kb messages. 292 + */ 293 + return (int)dg_size; 294 + } 295 + 296 + /* 297 + * Dispatch datagram as a guest, down through the VMX and potentially to 298 + * the host. 299 + * Returns number of bytes sent on success, error code otherwise. 300 + */ 301 + static int dg_dispatch_as_guest(struct vmci_datagram *dg) 302 + { 303 + int retval; 304 + struct vmci_resource *resource; 305 + 306 + resource = vmci_resource_by_handle(dg->src, 307 + VMCI_RESOURCE_TYPE_DATAGRAM); 308 + if (!resource) 309 + return VMCI_ERROR_NO_HANDLE; 310 + 311 + retval = vmci_send_datagram(dg); 312 + vmci_resource_put(resource); 313 + return retval; 314 + } 315 + 316 + /* 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. 320 + */ 321 + int vmci_datagram_dispatch(u32 context_id, 322 + struct vmci_datagram *dg, bool from_guest) 323 + { 324 + int retval; 325 + enum vmci_route route; 326 + 327 + BUILD_BUG_ON(sizeof(struct vmci_datagram) != 24); 328 + 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; 333 + } 334 + 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); 339 + return retval; 340 + } 341 + 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); 346 + } 347 + 348 + if (VMCI_ROUTE_AS_GUEST == route) 349 + return dg_dispatch_as_guest(dg); 350 + 351 + pr_warn("Unknown route (%d) for datagram\n", route); 352 + return VMCI_ERROR_DST_UNREACHABLE; 353 + } 354 + 355 + /* 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 358 + * virtual device. 359 + */ 360 + int vmci_datagram_invoke_guest_handler(struct vmci_datagram *dg) 361 + { 362 + struct vmci_resource *resource; 363 + struct datagram_entry *dst_entry; 364 + 365 + resource = vmci_resource_by_handle(dg->dst, 366 + VMCI_RESOURCE_TYPE_DATAGRAM); 367 + if (!resource) { 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; 371 + } 372 + 373 + dst_entry = container_of(resource, struct datagram_entry, resource); 374 + if (dst_entry->run_delayed) { 375 + struct delayed_datagram_info *dg_info; 376 + 377 + dg_info = kmalloc(sizeof(*dg_info) + (size_t)dg->payload_size, 378 + GFP_ATOMIC); 379 + if (!dg_info) { 380 + vmci_resource_put(resource); 381 + return VMCI_ERROR_NO_MEM; 382 + } 383 + 384 + dg_info->in_dg_host_queue = false; 385 + dg_info->entry = dst_entry; 386 + memcpy(&dg_info->msg, dg, VMCI_DG_SIZE(dg)); 387 + 388 + INIT_WORK(&dg_info->work, dg_delayed_dispatch); 389 + schedule_work(&dg_info->work); 390 + } else { 391 + dst_entry->recv_cb(dst_entry->client_data, dg); 392 + vmci_resource_put(resource); 393 + } 394 + 395 + return VMCI_SUCCESS; 396 + } 397 + 398 + /* 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. 406 + * 407 + * Creates a host context datagram endpoint and returns a handle to it. 408 + */ 409 + int vmci_datagram_create_handle_priv(u32 resource_id, 410 + u32 flags, 411 + u32 priv_flags, 412 + vmci_datagram_recv_cb recv_cb, 413 + void *client_data, 414 + struct vmci_handle *out_handle) 415 + { 416 + if (out_handle == NULL) 417 + return VMCI_ERROR_INVALID_ARGS; 418 + 419 + if (recv_cb == NULL) { 420 + pr_devel("Client callback needed when creating datagram\n"); 421 + return VMCI_ERROR_INVALID_ARGS; 422 + } 423 + 424 + if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS) 425 + return VMCI_ERROR_INVALID_ARGS; 426 + 427 + return dg_create_handle(resource_id, flags, priv_flags, recv_cb, 428 + client_data, out_handle); 429 + } 430 + EXPORT_SYMBOL_GPL(vmci_datagram_create_handle_priv); 431 + 432 + /* 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. 439 + * 440 + * Creates a host context datagram endpoint and returns a handle to 441 + * it. Same as vmci_datagram_create_handle_priv without the priviledge 442 + * flags argument. 443 + */ 444 + int vmci_datagram_create_handle(u32 resource_id, 445 + u32 flags, 446 + vmci_datagram_recv_cb recv_cb, 447 + void *client_data, 448 + struct vmci_handle *out_handle) 449 + { 450 + return vmci_datagram_create_handle_priv( 451 + resource_id, flags, 452 + VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS, 453 + recv_cb, client_data, 454 + out_handle); 455 + } 456 + EXPORT_SYMBOL_GPL(vmci_datagram_create_handle); 457 + 458 + /* 459 + * vmci_datagram_destroy_handle() - Destroys datagram handle 460 + * @handle: vmci_handle to be destroyed and reaped. 461 + * 462 + * Use this function to destroy any datagram handles created by 463 + * vmci_datagram_create_handle{,Priv} functions. 464 + */ 465 + int vmci_datagram_destroy_handle(struct vmci_handle handle) 466 + { 467 + struct datagram_entry *entry; 468 + struct vmci_resource *resource; 469 + 470 + resource = vmci_resource_by_handle(handle, VMCI_RESOURCE_TYPE_DATAGRAM); 471 + if (!resource) { 472 + pr_devel("Failed to destroy datagram (handle=0x%x:0x%x)\n", 473 + handle.context, handle.resource); 474 + return VMCI_ERROR_NOT_FOUND; 475 + } 476 + 477 + entry = container_of(resource, struct datagram_entry, resource); 478 + 479 + vmci_resource_put(&entry->resource); 480 + vmci_resource_remove(&entry->resource); 481 + kfree(entry); 482 + 483 + return VMCI_SUCCESS; 484 + } 485 + EXPORT_SYMBOL_GPL(vmci_datagram_destroy_handle); 486 + 487 + /* 488 + * vmci_datagram_send() - Send a datagram 489 + * @msg: The datagram to send. 490 + * 491 + * Sends the provided datagram on its merry way. 492 + */ 493 + int vmci_datagram_send(struct vmci_datagram *msg) 494 + { 495 + if (msg == NULL) 496 + return VMCI_ERROR_INVALID_ARGS; 497 + 498 + return vmci_datagram_dispatch(VMCI_INVALID_ID, msg, false); 499 + } 500 + EXPORT_SYMBOL_GPL(vmci_datagram_send);
+52
drivers/misc/vmw_vmci/vmci_datagram.h
··· 1 + /* 2 + * VMware VMCI Driver 3 + * 4 + * Copyright (C) 2012 VMware, Inc. All rights reserved. 5 + * 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. 9 + * 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 13 + * for more details. 14 + */ 15 + 16 + #ifndef _VMCI_DATAGRAM_H_ 17 + #define _VMCI_DATAGRAM_H_ 18 + 19 + #include <linux/types.h> 20 + #include <linux/list.h> 21 + 22 + #include "vmci_context.h" 23 + 24 + #define VMCI_MAX_DELAYED_DG_HOST_QUEUE_SIZE 256 25 + 26 + /* 27 + * The struct vmci_datagram_queue_entry is a queue header for the in-kernel VMCI 28 + * datagram queues. It is allocated in non-paged memory, as the 29 + * content is accessed while holding a spinlock. The pending datagram 30 + * itself may be allocated from paged memory. We shadow the size of 31 + * the datagram in the non-paged queue entry as this size is used 32 + * while holding the same spinlock as above. 33 + */ 34 + struct vmci_datagram_queue_entry { 35 + struct list_head list_item; /* For queuing. */ 36 + size_t dg_size; /* Size of datagram. */ 37 + struct vmci_datagram *dg; /* Pending datagram. */ 38 + }; 39 + 40 + /* VMCIDatagramSendRecvInfo */ 41 + struct vmci_datagram_snd_rcv_info { 42 + u64 addr; 43 + u32 len; 44 + s32 result; 45 + }; 46 + 47 + /* Datagram API for non-public use. */ 48 + int vmci_datagram_dispatch(u32 context_id, struct vmci_datagram *dg, 49 + bool from_guest); 50 + int vmci_datagram_invoke_guest_handler(struct vmci_datagram *dg); 51 + 52 + #endif /* _VMCI_DATAGRAM_H_ */