2 * Copyright (c) 2018 VMware, Inc.
4 * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
7 /* VMCI QueuePair API implementation. */
10 __FBSDID("$FreeBSD$");
13 #include "vmci_driver.h"
14 #include "vmci_event.h"
15 #include "vmci_kernel_api.h"
16 #include "vmci_kernel_defs.h"
17 #include "vmci_queue_pair.h"
19 #define LGPFX "vmci_queue_pair: "
21 struct queue_pair_entry {
22 vmci_list_item(queue_pair_entry) list_item;
23 struct vmci_handle handle;
26 uint64_t produce_size;
27 uint64_t consume_size;
31 struct qp_guest_endpoint {
32 struct queue_pair_entry qp;
36 bool hibernate_failure;
37 struct ppn_set ppn_set;
40 struct queue_pair_list {
41 vmci_list(queue_pair_entry) head;
42 volatile int hibernate;
46 #define QPE_NUM_PAGES(_QPE) \
47 ((uint32_t)(CEILING(_QPE.produce_size, PAGE_SIZE) + \
48 CEILING(_QPE.consume_size, PAGE_SIZE) + 2))
50 static struct queue_pair_list qp_guest_endpoints;
52 static struct queue_pair_entry *queue_pair_list_find_entry(
53 struct queue_pair_list *qp_list, struct vmci_handle handle);
54 static void queue_pair_list_add_entry(struct queue_pair_list *qp_list,
55 struct queue_pair_entry *entry);
56 static void queue_pair_list_remove_entry(struct queue_pair_list *qp_list,
57 struct queue_pair_entry *entry);
58 static struct queue_pair_entry *queue_pair_list_get_head(
59 struct queue_pair_list *qp_list);
60 static int queue_pair_notify_peer_local(bool attach,
61 struct vmci_handle handle);
62 static struct qp_guest_endpoint *qp_guest_endpoint_create(
63 struct vmci_handle handle, vmci_id peer, uint32_t flags,
64 uint64_t produce_size, uint64_t consume_size,
65 void *produce_q, void *consume_q);
66 static void qp_guest_endpoint_destroy(struct qp_guest_endpoint *entry);
67 static int vmci_queue_pair_alloc_hypercall(
68 const struct qp_guest_endpoint *entry);
69 static int vmci_queue_pair_alloc_guest_work(struct vmci_handle *handle,
70 struct vmci_queue **produce_q, uint64_t produce_size,
71 struct vmci_queue **consume_q, uint64_t consume_size,
72 vmci_id peer, uint32_t flags,
73 vmci_privilege_flags priv_flags);
74 static int vmci_queue_pair_detach_guest_work(struct vmci_handle handle);
75 static int vmci_queue_pair_detach_hypercall(struct vmci_handle handle);
78 *------------------------------------------------------------------------------
80 * vmci_queue_pair_alloc --
82 * Allocates a VMCI QueuePair. Only checks validity of input arguments. The
83 * real work is done in the host or guest specific function.
86 * VMCI_SUCCESS on success, appropriate error code otherwise.
91 *------------------------------------------------------------------------------
95 vmci_queue_pair_alloc(struct vmci_handle *handle, struct vmci_queue **produce_q,
96 uint64_t produce_size, struct vmci_queue **consume_q, uint64_t consume_size,
97 vmci_id peer, uint32_t flags, vmci_privilege_flags priv_flags)
100 if (!handle || !produce_q || !consume_q ||
101 (!produce_size && !consume_size) || (flags & ~VMCI_QP_ALL_FLAGS))
102 return (VMCI_ERROR_INVALID_ARGS);
104 return (vmci_queue_pair_alloc_guest_work(handle, produce_q,
105 produce_size, consume_q, consume_size, peer, flags, priv_flags));
109 *------------------------------------------------------------------------------
111 * vmci_queue_pair_detach --
113 * Detaches from a VMCI QueuePair. Only checks validity of input argument.
114 * Real work is done in the host or guest specific function.
117 * Success or failure.
122 *------------------------------------------------------------------------------
126 vmci_queue_pair_detach(struct vmci_handle handle)
129 if (VMCI_HANDLE_INVALID(handle))
130 return (VMCI_ERROR_INVALID_ARGS);
132 return (vmci_queue_pair_detach_guest_work(handle));
136 *------------------------------------------------------------------------------
138 * queue_pair_list_init --
140 * Initializes the list of QueuePairs.
143 * Success or failure.
148 *------------------------------------------------------------------------------
152 queue_pair_list_init(struct queue_pair_list *qp_list)
156 vmci_list_init(&qp_list->head);
157 atomic_store_int(&qp_list->hibernate, 0);
158 ret = vmci_mutex_init(&qp_list->mutex, "VMCI QP List lock");
163 *------------------------------------------------------------------------------
165 * queue_pair_list_destroy --
167 * Destroy the list's mutex.
175 *------------------------------------------------------------------------------
179 queue_pair_list_destroy(struct queue_pair_list *qp_list)
182 vmci_mutex_destroy(&qp_list->mutex);
183 vmci_list_init(&qp_list->head);
187 *------------------------------------------------------------------------------
189 * queue_pair_list_find_entry --
191 * Finds the entry in the list corresponding to a given handle. Assumes that
192 * the list is locked.
200 *------------------------------------------------------------------------------
203 static struct queue_pair_entry *
204 queue_pair_list_find_entry(struct queue_pair_list *qp_list,
205 struct vmci_handle handle)
207 struct queue_pair_entry *next;
209 if (VMCI_HANDLE_INVALID(handle))
212 vmci_list_scan(next, &qp_list->head, list_item) {
213 if (VMCI_HANDLE_EQUAL(next->handle, handle))
221 *------------------------------------------------------------------------------
223 * queue_pair_list_add_entry --
225 * Adds the given entry to the list. Assumes that the list is locked.
233 *------------------------------------------------------------------------------
237 queue_pair_list_add_entry(struct queue_pair_list *qp_list,
238 struct queue_pair_entry *entry)
242 vmci_list_insert(&qp_list->head, entry, list_item);
246 *------------------------------------------------------------------------------
248 * queue_pair_list_remove_entry --
250 * Removes the given entry from the list. Assumes that the list is locked.
258 *------------------------------------------------------------------------------
262 queue_pair_list_remove_entry(struct queue_pair_list *qp_list,
263 struct queue_pair_entry *entry)
267 vmci_list_remove(entry, list_item);
271 *------------------------------------------------------------------------------
273 * queue_pair_list_get_head --
275 * Returns the entry from the head of the list. Assumes that the list is
284 *------------------------------------------------------------------------------
287 static struct queue_pair_entry *
288 queue_pair_list_get_head(struct queue_pair_list *qp_list)
291 return (vmci_list_first(&qp_list->head));
295 *------------------------------------------------------------------------------
297 * vmci_qp_guest_endpoints_init --
299 * Initalizes data structure state keeping track of queue pair guest
303 * VMCI_SUCCESS on success and appropriate failure code otherwise.
308 *------------------------------------------------------------------------------
312 vmci_qp_guest_endpoints_init(void)
315 return (queue_pair_list_init(&qp_guest_endpoints));
319 *------------------------------------------------------------------------------
321 * vmci_qp_guest_endpoints_exit --
323 * Destroys all guest queue pair endpoints. If active guest queue pairs
324 * still exist, hypercalls to attempt detach from these queue pairs will be
325 * made. Any failure to detach is silently ignored.
333 *------------------------------------------------------------------------------
337 vmci_qp_guest_endpoints_exit(void)
339 struct qp_guest_endpoint *entry;
341 vmci_mutex_acquire(&qp_guest_endpoints.mutex);
344 (struct qp_guest_endpoint *)queue_pair_list_get_head(
345 &qp_guest_endpoints)) != NULL) {
347 * Don't make a hypercall for local QueuePairs.
349 if (!(entry->qp.flags & VMCI_QPFLAG_LOCAL))
350 vmci_queue_pair_detach_hypercall(entry->qp.handle);
352 * We cannot fail the exit, so let's reset ref_count.
354 entry->qp.ref_count = 0;
355 queue_pair_list_remove_entry(&qp_guest_endpoints, &entry->qp);
356 qp_guest_endpoint_destroy(entry);
359 atomic_store_int(&qp_guest_endpoints.hibernate, 0);
360 vmci_mutex_release(&qp_guest_endpoints.mutex);
361 queue_pair_list_destroy(&qp_guest_endpoints);
365 *------------------------------------------------------------------------------
367 * vmci_qp_guest_endpoints_sync --
369 * Use this as a synchronization point when setting globals, for example,
370 * during device shutdown.
378 *------------------------------------------------------------------------------
382 vmci_qp_guest_endpoints_sync(void)
385 vmci_mutex_acquire(&qp_guest_endpoints.mutex);
386 vmci_mutex_release(&qp_guest_endpoints.mutex);
390 *------------------------------------------------------------------------------
392 * qp_guest_endpoint_create --
394 * Allocates and initializes a qp_guest_endpoint structure. Allocates a
395 * QueuePair rid (and handle) iff the given entry has an invalid handle.
396 * 0 through VMCI_RESERVED_RESOURCE_ID_MAX are reserved handles. Assumes
397 * that the QP list mutex is held by the caller.
400 * Pointer to structure intialized.
405 *------------------------------------------------------------------------------
408 struct qp_guest_endpoint *
409 qp_guest_endpoint_create(struct vmci_handle handle, vmci_id peer,
410 uint32_t flags, uint64_t produce_size, uint64_t consume_size,
411 void *produce_q, void *consume_q)
413 struct qp_guest_endpoint *entry;
414 static vmci_id queue_pair_rid;
415 const uint64_t num_ppns = CEILING(produce_size, PAGE_SIZE) +
416 CEILING(consume_size, PAGE_SIZE) +
417 2; /* One page each for the queue headers. */
419 queue_pair_rid = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
421 ASSERT((produce_size || consume_size) && produce_q && consume_q);
423 if (VMCI_HANDLE_INVALID(handle)) {
424 vmci_id context_id = vmci_get_context_id();
425 vmci_id old_rid = queue_pair_rid;
428 * Generate a unique QueuePair rid. Keep on trying until we
429 * wrap around in the RID space.
431 ASSERT(old_rid > VMCI_RESERVED_RESOURCE_ID_MAX);
433 handle = VMCI_MAKE_HANDLE(context_id, queue_pair_rid);
435 (struct qp_guest_endpoint *)
436 queue_pair_list_find_entry(&qp_guest_endpoints,
439 if (UNLIKELY(!queue_pair_rid)) {
441 * Skip the reserved rids.
444 VMCI_RESERVED_RESOURCE_ID_MAX + 1;
446 } while (entry && queue_pair_rid != old_rid);
448 if (UNLIKELY(entry != NULL)) {
449 ASSERT(queue_pair_rid == old_rid);
451 * We wrapped around --- no rids were free.
457 ASSERT(!VMCI_HANDLE_INVALID(handle) &&
458 queue_pair_list_find_entry(&qp_guest_endpoints, handle) == NULL);
459 entry = vmci_alloc_kernel_mem(sizeof(*entry), VMCI_MEMORY_NORMAL);
461 entry->qp.handle = handle;
462 entry->qp.peer = peer;
463 entry->qp.flags = flags;
464 entry->qp.produce_size = produce_size;
465 entry->qp.consume_size = consume_size;
466 entry->qp.ref_count = 0;
467 entry->num_ppns = num_ppns;
468 memset(&entry->ppn_set, 0, sizeof(entry->ppn_set));
469 entry->produce_q = produce_q;
470 entry->consume_q = consume_q;
476 *------------------------------------------------------------------------------
478 * qp_guest_endpoint_destroy --
480 * Frees a qp_guest_endpoint structure.
488 *------------------------------------------------------------------------------
492 qp_guest_endpoint_destroy(struct qp_guest_endpoint *entry)
496 ASSERT(entry->qp.ref_count == 0);
498 vmci_free_ppn_set(&entry->ppn_set);
499 vmci_free_queue(entry->produce_q, entry->qp.produce_size);
500 vmci_free_queue(entry->consume_q, entry->qp.consume_size);
501 vmci_free_kernel_mem(entry, sizeof(*entry));
505 *------------------------------------------------------------------------------
507 * vmci_queue_pair_alloc_hypercall --
509 * Helper to make a QueuePairAlloc hypercall when the driver is
510 * supporting a guest device.
513 * Result of the hypercall.
516 * Memory is allocated & freed.
518 *------------------------------------------------------------------------------
521 vmci_queue_pair_alloc_hypercall(const struct qp_guest_endpoint *entry)
523 struct vmci_queue_pair_alloc_msg *alloc_msg;
527 if (!entry || entry->num_ppns <= 2)
528 return (VMCI_ERROR_INVALID_ARGS);
530 ASSERT(!(entry->qp.flags & VMCI_QPFLAG_LOCAL));
532 msg_size = sizeof(*alloc_msg) + (size_t)entry->num_ppns * sizeof(PPN);
533 alloc_msg = vmci_alloc_kernel_mem(msg_size, VMCI_MEMORY_NORMAL);
535 return (VMCI_ERROR_NO_MEM);
537 alloc_msg->hdr.dst = VMCI_MAKE_HANDLE(VMCI_HYPERVISOR_CONTEXT_ID,
538 VMCI_QUEUEPAIR_ALLOC);
539 alloc_msg->hdr.src = VMCI_ANON_SRC_HANDLE;
540 alloc_msg->hdr.payload_size = msg_size - VMCI_DG_HEADERSIZE;
541 alloc_msg->handle = entry->qp.handle;
542 alloc_msg->peer = entry->qp.peer;
543 alloc_msg->flags = entry->qp.flags;
544 alloc_msg->produce_size = entry->qp.produce_size;
545 alloc_msg->consume_size = entry->qp.consume_size;
546 alloc_msg->num_ppns = entry->num_ppns;
547 result = vmci_populate_ppn_list((uint8_t *)alloc_msg +
548 sizeof(*alloc_msg), &entry->ppn_set);
549 if (result == VMCI_SUCCESS)
550 result = vmci_send_datagram((struct vmci_datagram *)alloc_msg);
551 vmci_free_kernel_mem(alloc_msg, msg_size);
557 *------------------------------------------------------------------------------
559 * vmci_queue_pair_alloc_guest_work --
561 * This functions handles the actual allocation of a VMCI queue pair guest
562 * endpoint. Allocates physical pages for the queue pair. It makes OS
563 * dependent calls through generic wrappers.
566 * Success or failure.
569 * Memory is allocated.
571 *------------------------------------------------------------------------------
575 vmci_queue_pair_alloc_guest_work(struct vmci_handle *handle,
576 struct vmci_queue **produce_q, uint64_t produce_size,
577 struct vmci_queue **consume_q, uint64_t consume_size, vmci_id peer,
578 uint32_t flags, vmci_privilege_flags priv_flags)
580 struct qp_guest_endpoint *queue_pair_entry = NULL;
581 void *my_consume_q = NULL;
582 void *my_produce_q = NULL;
583 const uint64_t num_consume_pages = CEILING(consume_size, PAGE_SIZE) + 1;
584 const uint64_t num_produce_pages = CEILING(produce_size, PAGE_SIZE) + 1;
587 ASSERT(handle && produce_q && consume_q &&
588 (produce_size || consume_size));
590 if (priv_flags != VMCI_NO_PRIVILEGE_FLAGS)
591 return (VMCI_ERROR_NO_ACCESS);
593 vmci_mutex_acquire(&qp_guest_endpoints.mutex);
595 if ((atomic_load_int(&qp_guest_endpoints.hibernate) == 1) &&
596 !(flags & VMCI_QPFLAG_LOCAL)) {
598 * While guest OS is in hibernate state, creating non-local
599 * queue pairs is not allowed after the point where the VMCI
600 * guest driver converted the existing queue pairs to local
604 result = VMCI_ERROR_UNAVAILABLE;
608 if ((queue_pair_entry =
609 (struct qp_guest_endpoint *)queue_pair_list_find_entry(
610 &qp_guest_endpoints, *handle)) != NULL) {
611 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
612 /* Local attach case. */
613 if (queue_pair_entry->qp.ref_count > 1) {
614 VMCI_LOG_DEBUG(LGPFX"Error attempting to "
615 "attach more than once.\n");
616 result = VMCI_ERROR_UNAVAILABLE;
617 goto error_keep_entry;
620 if (queue_pair_entry->qp.produce_size != consume_size ||
621 queue_pair_entry->qp.consume_size != produce_size ||
622 queue_pair_entry->qp.flags !=
623 (flags & ~VMCI_QPFLAG_ATTACH_ONLY)) {
624 VMCI_LOG_DEBUG(LGPFX"Error mismatched "
625 "queue pair in local attach.\n");
626 result = VMCI_ERROR_QUEUEPAIR_MISMATCH;
627 goto error_keep_entry;
631 * Do a local attach. We swap the consume and produce
632 * queues for the attacher and deliver an attach event.
634 result = queue_pair_notify_peer_local(true, *handle);
635 if (result < VMCI_SUCCESS)
636 goto error_keep_entry;
637 my_produce_q = queue_pair_entry->consume_q;
638 my_consume_q = queue_pair_entry->produce_q;
641 result = VMCI_ERROR_ALREADY_EXISTS;
642 goto error_keep_entry;
645 my_produce_q = vmci_alloc_queue(produce_size, flags);
647 VMCI_LOG_WARNING(LGPFX"Error allocating pages for produce "
649 result = VMCI_ERROR_NO_MEM;
653 my_consume_q = vmci_alloc_queue(consume_size, flags);
655 VMCI_LOG_WARNING(LGPFX"Error allocating pages for consume "
657 result = VMCI_ERROR_NO_MEM;
661 queue_pair_entry = qp_guest_endpoint_create(*handle, peer, flags,
662 produce_size, consume_size, my_produce_q, my_consume_q);
663 if (!queue_pair_entry) {
664 VMCI_LOG_WARNING(LGPFX"Error allocating memory in %s.\n",
666 result = VMCI_ERROR_NO_MEM;
670 result = vmci_alloc_ppn_set(my_produce_q, num_produce_pages,
671 my_consume_q, num_consume_pages, &queue_pair_entry->ppn_set);
672 if (result < VMCI_SUCCESS) {
673 VMCI_LOG_WARNING(LGPFX"vmci_alloc_ppn_set failed.\n");
678 * It's only necessary to notify the host if this queue pair will be
679 * attached to from another context.
681 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
682 /* Local create case. */
683 vmci_id context_id = vmci_get_context_id();
686 * Enforce similar checks on local queue pairs as we do for
687 * regular ones. The handle's context must match the creator
688 * or attacher context id (here they are both the current
689 * context id) and the attach-only flag cannot exist during
690 * create. We also ensure specified peer is this context or
693 if (queue_pair_entry->qp.handle.context != context_id ||
694 (queue_pair_entry->qp.peer != VMCI_INVALID_ID &&
695 queue_pair_entry->qp.peer != context_id)) {
696 result = VMCI_ERROR_NO_ACCESS;
700 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_ATTACH_ONLY) {
701 result = VMCI_ERROR_NOT_FOUND;
705 result = vmci_queue_pair_alloc_hypercall(queue_pair_entry);
706 if (result < VMCI_SUCCESS) {
708 LGPFX"vmci_queue_pair_alloc_hypercall result = "
714 queue_pair_list_add_entry(&qp_guest_endpoints, &queue_pair_entry->qp);
717 queue_pair_entry->qp.ref_count++;
718 *handle = queue_pair_entry->qp.handle;
719 *produce_q = (struct vmci_queue *)my_produce_q;
720 *consume_q = (struct vmci_queue *)my_consume_q;
723 * We should initialize the queue pair header pages on a local queue
724 * pair create. For non-local queue pairs, the hypervisor initializes
725 * the header pages in the create step.
727 if ((queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) &&
728 queue_pair_entry->qp.ref_count == 1) {
729 vmci_queue_header_init((*produce_q)->q_header, *handle);
730 vmci_queue_header_init((*consume_q)->q_header, *handle);
733 vmci_mutex_release(&qp_guest_endpoints.mutex);
735 return (VMCI_SUCCESS);
738 vmci_mutex_release(&qp_guest_endpoints.mutex);
739 if (queue_pair_entry) {
740 /* The queues will be freed inside the destroy routine. */
741 qp_guest_endpoint_destroy(queue_pair_entry);
744 vmci_free_queue(my_produce_q, produce_size);
746 vmci_free_queue(my_consume_q, consume_size);
751 /* This path should only be used when an existing entry was found. */
752 ASSERT(queue_pair_entry->qp.ref_count > 0);
753 vmci_mutex_release(&qp_guest_endpoints.mutex);
758 *------------------------------------------------------------------------------
760 * vmci_queue_pair_detach_hypercall --
762 * Helper to make a QueuePairDetach hypercall when the driver is supporting
766 * Result of the hypercall.
771 *------------------------------------------------------------------------------
775 vmci_queue_pair_detach_hypercall(struct vmci_handle handle)
777 struct vmci_queue_pair_detach_msg detach_msg;
779 detach_msg.hdr.dst = VMCI_MAKE_HANDLE(VMCI_HYPERVISOR_CONTEXT_ID,
780 VMCI_QUEUEPAIR_DETACH);
781 detach_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
782 detach_msg.hdr.payload_size = sizeof(handle);
783 detach_msg.handle = handle;
785 return (vmci_send_datagram((struct vmci_datagram *)&detach_msg));
789 *------------------------------------------------------------------------------
791 * vmci_queue_pair_detach_guest_work --
793 * Helper for VMCI QueuePair detach interface. Frees the physical pages for
797 * Success or failure.
800 * Memory may be freed.
802 *------------------------------------------------------------------------------
806 vmci_queue_pair_detach_guest_work(struct vmci_handle handle)
808 struct qp_guest_endpoint *entry;
812 ASSERT(!VMCI_HANDLE_INVALID(handle));
814 vmci_mutex_acquire(&qp_guest_endpoints.mutex);
816 entry = (struct qp_guest_endpoint *)queue_pair_list_find_entry(
817 &qp_guest_endpoints, handle);
819 vmci_mutex_release(&qp_guest_endpoints.mutex);
820 return (VMCI_ERROR_NOT_FOUND);
823 ASSERT(entry->qp.ref_count >= 1);
825 if (entry->qp.flags & VMCI_QPFLAG_LOCAL) {
826 result = VMCI_SUCCESS;
828 if (entry->qp.ref_count > 1) {
829 result = queue_pair_notify_peer_local(false, handle);
832 * We can fail to notify a local queuepair because we
833 * can't allocate. We still want to release the entry
834 * if that happens, so don't bail out yet.
838 result = vmci_queue_pair_detach_hypercall(handle);
839 if (entry->hibernate_failure) {
840 if (result == VMCI_ERROR_NOT_FOUND) {
842 * If a queue pair detach failed when entering
843 * hibernation, the guest driver and the device
844 * may disagree on its existence when coming
845 * out of hibernation. The guest driver will
846 * regard it as a non-local queue pair, but
847 * the device state is gone, since the device
848 * has been powered off. In this case, we
849 * treat the queue pair as a local queue pair
853 ASSERT(entry->qp.ref_count == 1);
854 result = VMCI_SUCCESS;
857 if (result < VMCI_SUCCESS) {
859 * We failed to notify a non-local queuepair. That other
860 * queuepair might still be accessing the shared
861 * memory, so don't release the entry yet. It will get
862 * cleaned up by vmci_queue_pair_Exit() if necessary
863 * (assuming we are going away, otherwise why did this
867 vmci_mutex_release(&qp_guest_endpoints.mutex);
873 * If we get here then we either failed to notify a local queuepair, or
874 * we succeeded in all cases. Release the entry if required.
877 entry->qp.ref_count--;
878 if (entry->qp.ref_count == 0)
879 queue_pair_list_remove_entry(&qp_guest_endpoints, &entry->qp);
881 /* If we didn't remove the entry, this could change once we unlock. */
882 ref_count = entry ? entry->qp.ref_count :
884 * Value does not matter, silence the
888 vmci_mutex_release(&qp_guest_endpoints.mutex);
891 qp_guest_endpoint_destroy(entry);
896 *------------------------------------------------------------------------------
898 * queue_pair_notify_peer_local --
900 * Dispatches a queue pair event message directly into the local event
904 * VMCI_SUCCESS on success, error code otherwise
909 *------------------------------------------------------------------------------
913 queue_pair_notify_peer_local(bool attach, struct vmci_handle handle)
915 struct vmci_event_msg *e_msg;
916 struct vmci_event_payload_qp *e_payload;
917 /* buf is only 48 bytes. */
919 context_id = vmci_get_context_id();
920 char buf[sizeof(*e_msg) + sizeof(*e_payload)];
922 e_msg = (struct vmci_event_msg *)buf;
923 e_payload = vmci_event_msg_payload(e_msg);
925 e_msg->hdr.dst = VMCI_MAKE_HANDLE(context_id, VMCI_EVENT_HANDLER);
926 e_msg->hdr.src = VMCI_MAKE_HANDLE(VMCI_HYPERVISOR_CONTEXT_ID,
927 VMCI_CONTEXT_RESOURCE_ID);
928 e_msg->hdr.payload_size = sizeof(*e_msg) + sizeof(*e_payload) -
930 e_msg->event_data.event = attach ? VMCI_EVENT_QP_PEER_ATTACH :
931 VMCI_EVENT_QP_PEER_DETACH;
932 e_payload->peer_id = context_id;
933 e_payload->handle = handle;
935 return (vmci_event_dispatch((struct vmci_datagram *)e_msg));