2 * Copyright (c) 2009-2012 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
32 #include "hv_vmbus_priv.h"
34 typedef void (*hv_pfn_channel_msg_handler)(hv_vmbus_channel_msg_header* msg);
36 typedef struct hv_vmbus_channel_msg_table_entry {
37 hv_vmbus_channel_msg_type messageType;
38 hv_pfn_channel_msg_handler messageHandler;
39 } hv_vmbus_channel_msg_table_entry;
45 static void vmbus_channel_on_offer(hv_vmbus_channel_msg_header* hdr);
46 static void vmbus_channel_on_open_result(hv_vmbus_channel_msg_header* hdr);
47 static void vmbus_channel_on_offer_rescind(hv_vmbus_channel_msg_header* hdr);
48 static void vmbus_channel_on_gpadl_created(hv_vmbus_channel_msg_header* hdr);
49 static void vmbus_channel_on_gpadl_torndown(hv_vmbus_channel_msg_header* hdr);
50 static void vmbus_channel_on_offers_delivered(hv_vmbus_channel_msg_header* hdr);
51 static void vmbus_channel_on_version_response(hv_vmbus_channel_msg_header* hdr);
52 static void vmbus_channel_process_offer(void *context);
53 struct hv_vmbus_channel*
54 vmbus_select_outgoing_channel(struct hv_vmbus_channel *promary);
57 * Channel message dispatch table
59 hv_vmbus_channel_msg_table_entry
60 g_channel_message_table[HV_CHANNEL_MESSAGE_COUNT] = {
61 { HV_CHANNEL_MESSAGE_INVALID, NULL },
62 { HV_CHANNEL_MESSAGE_OFFER_CHANNEL, vmbus_channel_on_offer },
63 { HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER,
64 vmbus_channel_on_offer_rescind },
65 { HV_CHANNEL_MESSAGE_REQUEST_OFFERS, NULL },
66 { HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED,
67 vmbus_channel_on_offers_delivered },
68 { HV_CHANNEL_MESSAGE_OPEN_CHANNEL, NULL },
69 { HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT,
70 vmbus_channel_on_open_result },
71 { HV_CHANNEL_MESSAGE_CLOSE_CHANNEL, NULL },
72 { HV_CHANNEL_MESSAGEL_GPADL_HEADER, NULL },
73 { HV_CHANNEL_MESSAGE_GPADL_BODY, NULL },
74 { HV_CHANNEL_MESSAGE_GPADL_CREATED,
75 vmbus_channel_on_gpadl_created },
76 { HV_CHANNEL_MESSAGE_GPADL_TEARDOWN, NULL },
77 { HV_CHANNEL_MESSAGE_GPADL_TORNDOWN,
78 vmbus_channel_on_gpadl_torndown },
79 { HV_CHANNEL_MESSAGE_REL_ID_RELEASED, NULL },
80 { HV_CHANNEL_MESSAGE_INITIATED_CONTACT, NULL },
81 { HV_CHANNEL_MESSAGE_VERSION_RESPONSE,
82 vmbus_channel_on_version_response },
83 { HV_CHANNEL_MESSAGE_UNLOAD, NULL }
88 * Implementation of the work abstraction.
91 work_item_callback(void *work, int pending)
93 struct hv_work_item *w = (struct hv_work_item *)work;
96 * Serialize work execution.
98 if (w->wq->work_sema != NULL) {
99 sema_wait(w->wq->work_sema);
102 w->callback(w->context);
104 if (w->wq->work_sema != NULL) {
105 sema_post(w->wq->work_sema);
111 struct hv_work_queue*
112 hv_work_queue_create(char* name)
114 static unsigned int qid = 0;
117 struct hv_work_queue* wq;
119 wq = malloc(sizeof(struct hv_work_queue), M_DEVBUF, M_NOWAIT | M_ZERO);
120 KASSERT(wq != NULL, ("Error VMBUS: Failed to allocate work_queue\n"));
125 * We use work abstraction to handle messages
126 * coming from the host and these are typically offers.
127 * Some FreeBsd drivers appear to have a concurrency issue
128 * where probe/attach needs to be serialized. We ensure that
129 * by having only one thread process work elements in a
130 * specific queue by serializing work execution.
133 if (strcmp(name, "vmbusQ") == 0) {
135 } else { /* control */
138 * Initialize semaphore for this queue by pointing
139 * to the globale semaphore used for synchronizing all
142 wq->work_sema = &hv_vmbus_g_connection.control_sema;
145 sprintf(qname, "hv_%s_%u", name, qid);
148 * Fixme: FreeBSD 8.2 has a different prototype for
149 * taskqueue_create(), and for certain other taskqueue functions.
150 * We need to research the implications of these changes.
151 * Fixme: Not sure when the changes were introduced.
153 wq->queue = taskqueue_create(qname, M_NOWAIT, taskqueue_thread_enqueue,
155 #if __FreeBSD_version < 800000
160 if (wq->queue == NULL) {
165 if (taskqueue_start_threads(&wq->queue, 1, pri, "%s taskq", qname)) {
166 taskqueue_free(wq->queue);
177 hv_work_queue_close(struct hv_work_queue *wq)
180 * KYS: Need to drain the taskqueue
181 * before we close the hv_work_queue.
183 /*KYS: taskqueue_drain(wq->tq, ); */
184 taskqueue_free(wq->queue);
189 * @brief Create work item
193 struct hv_work_queue *wq,
194 void (*callback)(void *), void *context)
196 struct hv_work_item *w = malloc(sizeof(struct hv_work_item),
197 M_DEVBUF, M_NOWAIT | M_ZERO);
198 KASSERT(w != NULL, ("Error VMBUS: Failed to allocate WorkItem\n"));
202 w->callback = callback;
203 w->context = context;
206 TASK_INIT(&w->work, 0, work_item_callback, w);
208 return (taskqueue_enqueue(wq->queue, &w->work));
212 * @brief Rescind the offer by initiating a device removal
215 vmbus_channel_process_rescind_offer(void *context)
217 hv_vmbus_channel* channel = (hv_vmbus_channel*) context;
218 hv_vmbus_child_device_unregister(channel->device);
222 * @brief Allocate and initialize a vmbus channel object
225 hv_vmbus_allocate_channel(void)
227 hv_vmbus_channel* channel;
229 channel = (hv_vmbus_channel*) malloc(
230 sizeof(hv_vmbus_channel),
233 KASSERT(channel != NULL, ("Error VMBUS: Failed to allocate channel!"));
237 mtx_init(&channel->inbound_lock, "channel inbound", NULL, MTX_DEF);
238 mtx_init(&channel->sc_lock, "vmbus multi channel", NULL, MTX_DEF);
240 TAILQ_INIT(&channel->sc_list_anchor);
242 channel->control_work_queue = hv_work_queue_create("control");
244 if (channel->control_work_queue == NULL) {
245 mtx_destroy(&channel->inbound_lock);
246 free(channel, M_DEVBUF);
254 * @brief Release the vmbus channel object itself
257 ReleaseVmbusChannel(void *context)
259 hv_vmbus_channel* channel = (hv_vmbus_channel*) context;
260 hv_work_queue_close(channel->control_work_queue);
261 free(channel, M_DEVBUF);
265 * @brief Release the resources used by the vmbus channel object
268 hv_vmbus_free_vmbus_channel(hv_vmbus_channel* channel)
270 mtx_destroy(&channel->sc_lock);
271 mtx_destroy(&channel->inbound_lock);
273 * We have to release the channel's workqueue/thread in
274 * the vmbus's workqueue/thread context
275 * ie we can't destroy ourselves
277 hv_queue_work_item(hv_vmbus_g_connection.work_queue,
278 ReleaseVmbusChannel, (void *) channel);
282 * @brief Process the offer by creating a channel/device
283 * associated with this offer
286 vmbus_channel_process_offer(void *context)
288 hv_vmbus_channel* new_channel;
290 hv_vmbus_channel* channel;
293 new_channel = (hv_vmbus_channel*) context;
298 * Make sure this is a new offer
300 mtx_lock(&hv_vmbus_g_connection.channel_lock);
302 TAILQ_FOREACH(channel, &hv_vmbus_g_connection.channel_anchor,
305 if (memcmp(&channel->offer_msg.offer.interface_type,
306 &new_channel->offer_msg.offer.interface_type,
307 sizeof(hv_guid)) == 0 &&
308 memcmp(&channel->offer_msg.offer.interface_instance,
309 &new_channel->offer_msg.offer.interface_instance,
310 sizeof(hv_guid)) == 0) {
319 &hv_vmbus_g_connection.channel_anchor,
323 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
325 /*XXX add new channel to percpu_list */
329 * Check if this is a sub channel.
331 if (new_channel->offer_msg.offer.sub_channel_index != 0) {
333 * It is a sub channel offer, process it.
335 new_channel->primary_channel = channel;
336 mtx_lock(&channel->sc_lock);
338 &channel->sc_list_anchor,
341 mtx_unlock(&channel->sc_lock);
343 /* Insert new channel into channel_anchor. */
344 printf("Storvsc get multi-channel offer, rel=%u.\n",
345 new_channel->offer_msg.child_rel_id);
346 mtx_lock(&hv_vmbus_g_connection.channel_lock);
347 TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_anchor,
348 new_channel, list_entry);
349 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
352 printf("VMBUS: new multi-channel offer <%p>.\n",
355 /*XXX add it to percpu_list */
357 new_channel->state = HV_CHANNEL_OPEN_STATE;
358 if (channel->sc_creation_callback != NULL) {
359 channel->sc_creation_callback(new_channel);
364 hv_vmbus_free_vmbus_channel(new_channel);
368 new_channel->state = HV_CHANNEL_OPEN_STATE;
371 * Start the process of binding this offer to the driver
372 * (We need to set the device field before calling
373 * hv_vmbus_child_device_add())
375 new_channel->device = hv_vmbus_child_device_create(
376 new_channel->offer_msg.offer.interface_type,
377 new_channel->offer_msg.offer.interface_instance, new_channel);
380 * Add the new device to the bus. This will kick off device-driver
381 * binding which eventually invokes the device driver's AddDevice()
384 ret = hv_vmbus_child_device_register(new_channel->device);
386 mtx_lock(&hv_vmbus_g_connection.channel_lock);
388 &hv_vmbus_g_connection.channel_anchor,
391 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
392 hv_vmbus_free_vmbus_channel(new_channel);
397 * Array of device guids that are performance critical. We try to distribute
398 * the interrupt load for these devices across all online cpus.
400 static const hv_guid high_perf_devices[] = {
414 * We use this static number to distribute the channel interrupt load.
416 static uint32_t next_vcpu;
419 * Starting with Win8, we can statically distribute the incoming
420 * channel interrupt load by binding a channel to VCPU. We
421 * implement here a simple round robin scheme for distributing
422 * the interrupt load.
423 * We will bind channels that are not performance critical to cpu 0 and
424 * performance critical channels (IDE, SCSI and Network) will be uniformly
425 * distributed across all available CPUs.
428 vmbus_channel_select_cpu(hv_vmbus_channel *channel, hv_guid *guid)
430 uint32_t current_cpu;
432 boolean_t is_perf_channel = FALSE;
434 for (i = PERF_CHN_NIC; i < MAX_PERF_CHN; i++) {
435 if (memcmp(guid->data, high_perf_devices[i].data,
436 sizeof(hv_guid)) == 0) {
437 is_perf_channel = TRUE;
442 if ((hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008) ||
443 (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7) ||
444 (!is_perf_channel)) {
445 /* Host's view of guest cpu */
446 channel->target_vcpu = 0;
447 /* Guest's own view of cpu */
448 channel->target_cpu = 0;
451 /* mp_ncpus should have the number cpus currently online */
452 current_cpu = (++next_vcpu % mp_ncpus);
453 channel->target_cpu = current_cpu;
454 channel->target_vcpu =
455 hv_vmbus_g_context.hv_vcpu_index[current_cpu];
457 printf("VMBUS: Total online cpus %d, assign perf channel %d "
458 "to vcpu %d, cpu %d\n", mp_ncpus, i, channel->target_vcpu,
463 * @brief Handler for channel offers from Hyper-V/Azure
465 * Handler for channel offers from vmbus in parent partition. We ignore
466 * all offers except network and storage offers. For each network and storage
467 * offers, we create a channel object and queue a work item to the channel
468 * object to process the offer synchronously
471 vmbus_channel_on_offer(hv_vmbus_channel_msg_header* hdr)
473 hv_vmbus_channel_offer_channel* offer;
474 hv_vmbus_channel* new_channel;
476 offer = (hv_vmbus_channel_offer_channel*) hdr;
479 hv_guid *guidInstance;
481 guidType = &offer->offer.interface_type;
482 guidInstance = &offer->offer.interface_instance;
484 /* Allocate the channel object and save this offer */
485 new_channel = hv_vmbus_allocate_channel();
486 if (new_channel == NULL)
490 * By default we setup state to enable batched
491 * reading. A specific service can choose to
492 * disable this prior to opening the channel.
494 new_channel->batched_reading = TRUE;
496 new_channel->signal_event_param =
497 (hv_vmbus_input_signal_event *)
498 (HV_ALIGN_UP((unsigned long)
499 &new_channel->signal_event_buffer,
500 HV_HYPERCALL_PARAM_ALIGN));
502 new_channel->signal_event_param->connection_id.as_uint32_t = 0;
503 new_channel->signal_event_param->connection_id.u.id =
504 HV_VMBUS_EVENT_CONNECTION_ID;
505 new_channel->signal_event_param->flag_number = 0;
506 new_channel->signal_event_param->rsvd_z = 0;
508 if (hv_vmbus_protocal_version != HV_VMBUS_VERSION_WS2008) {
509 new_channel->is_dedicated_interrupt =
510 (offer->is_dedicated_interrupt != 0);
511 new_channel->signal_event_param->connection_id.u.id =
512 offer->connection_id;
516 * Bind the channel to a chosen cpu.
518 vmbus_channel_select_cpu(new_channel,
519 &offer->offer.interface_type);
521 memcpy(&new_channel->offer_msg, offer,
522 sizeof(hv_vmbus_channel_offer_channel));
523 new_channel->monitor_group = (uint8_t) offer->monitor_id / 32;
524 new_channel->monitor_bit = (uint8_t) offer->monitor_id % 32;
526 /* TODO: Make sure the offer comes from our parent partition */
528 new_channel->control_work_queue,
529 vmbus_channel_process_offer,
534 * @brief Rescind offer handler.
536 * We queue a work item to process this offer
540 vmbus_channel_on_offer_rescind(hv_vmbus_channel_msg_header* hdr)
542 hv_vmbus_channel_rescind_offer* rescind;
543 hv_vmbus_channel* channel;
545 rescind = (hv_vmbus_channel_rescind_offer*) hdr;
547 channel = hv_vmbus_get_channel_from_rel_id(rescind->child_rel_id);
551 hv_queue_work_item(channel->control_work_queue,
552 vmbus_channel_process_rescind_offer, channel);
557 * @brief Invoked when all offers have been delivered.
560 vmbus_channel_on_offers_delivered(hv_vmbus_channel_msg_header* hdr)
565 * @brief Open result handler.
567 * This is invoked when we received a response
568 * to our channel open request. Find the matching request, copy the
569 * response and signal the requesting thread.
572 vmbus_channel_on_open_result(hv_vmbus_channel_msg_header* hdr)
574 hv_vmbus_channel_open_result* result;
575 hv_vmbus_channel_msg_info* msg_info;
576 hv_vmbus_channel_msg_header* requestHeader;
577 hv_vmbus_channel_open_channel* openMsg;
579 result = (hv_vmbus_channel_open_result*) hdr;
582 * Find the open msg, copy the result and signal/unblock the wait event
584 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
586 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
588 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
590 if (requestHeader->message_type ==
591 HV_CHANNEL_MESSAGE_OPEN_CHANNEL) {
592 openMsg = (hv_vmbus_channel_open_channel*) msg_info->msg;
593 if (openMsg->child_rel_id == result->child_rel_id
594 && openMsg->open_id == result->open_id) {
595 memcpy(&msg_info->response.open_result, result,
596 sizeof(hv_vmbus_channel_open_result));
597 sema_post(&msg_info->wait_sema);
602 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
607 * @brief GPADL created handler.
609 * This is invoked when we received a response
610 * to our gpadl create request. Find the matching request, copy the
611 * response and signal the requesting thread.
614 vmbus_channel_on_gpadl_created(hv_vmbus_channel_msg_header* hdr)
616 hv_vmbus_channel_gpadl_created* gpadl_created;
617 hv_vmbus_channel_msg_info* msg_info;
618 hv_vmbus_channel_msg_header* request_header;
619 hv_vmbus_channel_gpadl_header* gpadl_header;
621 gpadl_created = (hv_vmbus_channel_gpadl_created*) hdr;
623 /* Find the establish msg, copy the result and signal/unblock
626 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
627 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
629 request_header = (hv_vmbus_channel_msg_header*) msg_info->msg;
630 if (request_header->message_type ==
631 HV_CHANNEL_MESSAGEL_GPADL_HEADER) {
633 (hv_vmbus_channel_gpadl_header*) request_header;
635 if ((gpadl_created->child_rel_id == gpadl_header->child_rel_id)
636 && (gpadl_created->gpadl == gpadl_header->gpadl)) {
637 memcpy(&msg_info->response.gpadl_created,
639 sizeof(hv_vmbus_channel_gpadl_created));
640 sema_post(&msg_info->wait_sema);
645 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
649 * @brief GPADL torndown handler.
651 * This is invoked when we received a respons
652 * to our gpadl teardown request. Find the matching request, copy the
653 * response and signal the requesting thread
656 vmbus_channel_on_gpadl_torndown(hv_vmbus_channel_msg_header* hdr)
658 hv_vmbus_channel_gpadl_torndown* gpadl_torndown;
659 hv_vmbus_channel_msg_info* msg_info;
660 hv_vmbus_channel_msg_header* requestHeader;
661 hv_vmbus_channel_gpadl_teardown* gpadlTeardown;
663 gpadl_torndown = (hv_vmbus_channel_gpadl_torndown*)hdr;
666 * Find the open msg, copy the result and signal/unblock the
670 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
672 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
674 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
676 if (requestHeader->message_type
677 == HV_CHANNEL_MESSAGE_GPADL_TEARDOWN) {
679 (hv_vmbus_channel_gpadl_teardown*) requestHeader;
681 if (gpadl_torndown->gpadl == gpadlTeardown->gpadl) {
682 memcpy(&msg_info->response.gpadl_torndown,
684 sizeof(hv_vmbus_channel_gpadl_torndown));
685 sema_post(&msg_info->wait_sema);
690 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
694 * @brief Version response handler.
696 * This is invoked when we received a response
697 * to our initiate contact request. Find the matching request, copy th
698 * response and signal the requesting thread.
701 vmbus_channel_on_version_response(hv_vmbus_channel_msg_header* hdr)
703 hv_vmbus_channel_msg_info* msg_info;
704 hv_vmbus_channel_msg_header* requestHeader;
705 hv_vmbus_channel_initiate_contact* initiate;
706 hv_vmbus_channel_version_response* versionResponse;
708 versionResponse = (hv_vmbus_channel_version_response*)hdr;
710 mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
711 TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
713 requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
714 if (requestHeader->message_type
715 == HV_CHANNEL_MESSAGE_INITIATED_CONTACT) {
717 (hv_vmbus_channel_initiate_contact*) requestHeader;
718 memcpy(&msg_info->response.version_response,
720 sizeof(hv_vmbus_channel_version_response));
721 sema_post(&msg_info->wait_sema);
724 mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
729 * @brief Handler for channel protocol messages.
731 * This is invoked in the vmbus worker thread context.
734 hv_vmbus_on_channel_message(void *context)
736 hv_vmbus_message* msg;
737 hv_vmbus_channel_msg_header* hdr;
740 msg = (hv_vmbus_message*) context;
741 hdr = (hv_vmbus_channel_msg_header*) msg->u.payload;
742 size = msg->header.payload_size;
744 if (hdr->message_type >= HV_CHANNEL_MESSAGE_COUNT) {
749 if (g_channel_message_table[hdr->message_type].messageHandler) {
750 g_channel_message_table[hdr->message_type].messageHandler(hdr);
753 /* Free the msg that was allocated in VmbusOnMsgDPC() */
758 * @brief Send a request to get all our pending offers.
761 hv_vmbus_request_channel_offers(void)
764 hv_vmbus_channel_msg_header* msg;
765 hv_vmbus_channel_msg_info* msg_info;
767 msg_info = (hv_vmbus_channel_msg_info *)
768 malloc(sizeof(hv_vmbus_channel_msg_info)
769 + sizeof(hv_vmbus_channel_msg_header), M_DEVBUF, M_NOWAIT);
771 if (msg_info == NULL) {
773 printf("Error VMBUS: malloc failed for Request Offers\n");
777 msg = (hv_vmbus_channel_msg_header*) msg_info->msg;
778 msg->message_type = HV_CHANNEL_MESSAGE_REQUEST_OFFERS;
780 ret = hv_vmbus_post_message(msg, sizeof(hv_vmbus_channel_msg_header));
783 free(msg_info, M_DEVBUF);
789 * @brief Release channels that are unattached/unconnected (i.e., no drivers associated)
792 hv_vmbus_release_unattached_channels(void)
794 hv_vmbus_channel *channel;
796 mtx_lock(&hv_vmbus_g_connection.channel_lock);
798 while (!TAILQ_EMPTY(&hv_vmbus_g_connection.channel_anchor)) {
799 channel = TAILQ_FIRST(&hv_vmbus_g_connection.channel_anchor);
800 TAILQ_REMOVE(&hv_vmbus_g_connection.channel_anchor,
801 channel, list_entry);
803 hv_vmbus_child_device_unregister(channel->device);
804 hv_vmbus_free_vmbus_channel(channel);
806 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
810 * @brief Select the best outgoing channel
812 * The channel whose vcpu binding is closest to the currect vcpu will
814 * If no multi-channel, always select primary channel
816 * @param primary - primary channel
818 struct hv_vmbus_channel *
819 vmbus_select_outgoing_channel(struct hv_vmbus_channel *primary)
821 hv_vmbus_channel *new_channel = NULL;
822 hv_vmbus_channel *outgoing_channel = primary;
823 int old_cpu_distance = 0;
824 int new_cpu_distance = 0;
826 int smp_pro_id = PCPU_GET(cpuid);
828 if (TAILQ_EMPTY(&primary->sc_list_anchor)) {
829 return outgoing_channel;
832 if (smp_pro_id >= MAXCPU) {
833 return outgoing_channel;
836 cur_vcpu = hv_vmbus_g_context.hv_vcpu_index[smp_pro_id];
838 TAILQ_FOREACH(new_channel, &primary->sc_list_anchor, sc_list_entry) {
839 if (new_channel->state != HV_CHANNEL_OPENED_STATE){
843 if (new_channel->target_vcpu == cur_vcpu){
847 old_cpu_distance = ((outgoing_channel->target_vcpu > cur_vcpu) ?
848 (outgoing_channel->target_vcpu - cur_vcpu) :
849 (cur_vcpu - outgoing_channel->target_vcpu));
851 new_cpu_distance = ((new_channel->target_vcpu > cur_vcpu) ?
852 (new_channel->target_vcpu - cur_vcpu) :
853 (cur_vcpu - new_channel->target_vcpu));
855 if (old_cpu_distance < new_cpu_distance) {
859 outgoing_channel = new_channel;
862 return(outgoing_channel);