QEMU源码全解析 —— virtio(12)

news/2025/2/23 9:02:03

接前一篇文章:

上一回对于virtio_device_realize函数进行了详细解析。在第2步中virtio_device_realize函数调用了具体类的realize函数,对于virtio balloon设备来说是virtio_balloon_realize函数。本回就来对于virtio_balloon_device_realize函数进行解析。

为了便于理解,再次贴出virtio_device_realize函数源码,在hw/virtio/virtio.c中,如下:

static void virtio_device_realize(DeviceState *dev, Error **errp)
{
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
    Error *err = NULL;
 
    /* Devices should either use vmsd or the load/save methods */
    assert(!vdc->vmsd || !vdc->load);
 
    if (vdc->realize != NULL) {
        vdc->realize(dev, &err);
        if (err != NULL) {
            error_propagate(errp, err);
            return;
        }
    }
 
    virtio_bus_device_plugged(vdev, &err);
    if (err != NULL) {
        error_propagate(errp, err);
        vdc->unrealize(dev);
        return;
    }
 
    vdev->listener.commit = virtio_memory_listener_commit;
    vdev->listener.name = "virtio";
    memory_listener_register(&vdev->listener, vdev->dma_as);
    QTAILQ_INSERT_TAIL(&virtio_list, vdev, next);
}

调用具体类的realize函数的代码片段如下:

    if (vdc->realize != NULL) {
        vdc->realize(dev, &err);
        if (err != NULL) {
            error_propagate(errp, err);
            return;
        }
    }

virtio_balloon_device_realize函数在hw/virtio/virtio-balloon.c中,代码如下:

static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
{
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
    VirtIOBalloon *s = VIRTIO_BALLOON(dev);
    int ret;

    virtio_init(vdev, VIRTIO_ID_BALLOON, virtio_balloon_config_size(s));

    ret = qemu_add_balloon_handler(virtio_balloon_to_target,
                                   virtio_balloon_stat, s);

    if (ret < 0) {
        error_setg(errp, "Only one balloon device is supported");
        virtio_cleanup(vdev);
        return;
    }

    if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_FREE_PAGE_HINT) &&
        !s->iothread) {
        error_setg(errp, "'free-page-hint' requires 'iothread' to be set");
        virtio_cleanup(vdev);
        return;
    }

    s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
    s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
    s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);

    if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
        s->free_page_vq = virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE,
                                           virtio_balloon_handle_free_page_vq);
        precopy_add_notifier(&s->free_page_hint_notify);

        object_ref(OBJECT(s->iothread));
        s->free_page_bh = aio_bh_new_guarded(iothread_get_aio_context(s->iothread),
                                             virtio_ballloon_get_free_page_hints, s,
                                             &dev->mem_reentrancy_guard);
    }

    if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_REPORTING)) {
        s->reporting_vq = virtio_add_queue(vdev, 32,
                                           virtio_balloon_handle_report);
    }

    reset_stats(s);
}

在同文件(hw/virtio/virtio-balloon.c)的virtio_balloon_class_init函数中,将virtio_balloon_device_realize函数(地址)赋给了vdc->realize。

static void virtio_balloon_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);

    device_class_set_props(dc, virtio_balloon_properties);
    dc->vmsd = &vmstate_virtio_balloon;
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
    vdc->realize = virtio_balloon_device_realize;
    vdc->unrealize = virtio_balloon_device_unrealize;
    vdc->reset = virtio_balloon_device_reset;
    vdc->get_config = virtio_balloon_get_config;
    vdc->set_config = virtio_balloon_set_config;
    vdc->get_features = virtio_balloon_get_features;
    vdc->set_status = virtio_balloon_set_status;
    vdc->vmsd = &vmstate_virtio_balloon_device;
}

virtio_balloon_device_realize()是virtio balloon设备的具现化函数,它用于实现TYPE_VIRTIO_BALLOON_DEVICE的具现化。

(1)virtio_balloon_device_realize函数首先调用virtio_init函数初始化virtio设备的公共部分。代码片段如下:

    virtio_init(vdev, VIRTIO_ID_BALLOON, virtio_balloon_config_size(s));

virtio_init函数在hw/virtio/virtio.c中,代码如下:

void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size)
{
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
    int i;
    int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;

    if (nvectors) {
        vdev->vector_queues =
            g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
    }

    vdev->start_on_kick = false;
    vdev->started = false;
    vdev->vhost_started = false;
    vdev->device_id = device_id;
    vdev->status = 0;
    qatomic_set(&vdev->isr, 0);
    vdev->queue_sel = 0;
    vdev->config_vector = VIRTIO_NO_VECTOR;
    vdev->vq = g_new0(VirtQueue, VIRTIO_QUEUE_MAX);
    vdev->vm_running = runstate_is_running();
    vdev->broken = false;
    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        vdev->vq[i].vector = VIRTIO_NO_VECTOR;
        vdev->vq[i].vdev = vdev;
        vdev->vq[i].queue_index = i;
        vdev->vq[i].host_notifier_enabled = false;
    }

    vdev->name = virtio_id_to_name(device_id);
    vdev->config_len = config_size;
    if (vdev->config_len) {
        vdev->config = g_malloc0(config_size);
    } else {
        vdev->config = NULL;
    }
    vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev),
            virtio_vmstate_change, vdev);
    vdev->device_endian = virtio_default_endian();
    vdev->use_guest_notifier_mask = true;
}

virtio_init函数的工作是初始化所有virtio设备的基类TYPE_VIRTIO_DEVICE的实例VirtIODevice结构体,其对VirtIODevice的成员进行初始化。

VirtIODevice的vector_queues成员和config_vector成员与MSI中断相关;device_id、status、name成员分别表示设备的id、状态和名字;isr成员用来表示中断请求;queue_sel成员用来在进行配置队列的时候选择队列;vq成员表示的是该设备的virtio queue,这里分配了VIRTIO_QUEUE_MAX个queue,并且进行了初始化;config_len和config分别表示该virtio设备配置空间的长度和数据存放区域;use_guest_notifier_mask成员与irqfd有关。

回到virtio_balloon_device_realize函数。

(2)在virtio_init函数初始化了VirtIODevice之后,调用virtio_add_queue函数创建了3个virtqueue。代码片段如下:

    s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
    s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
    s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);

virtqueue是virtio设备的重要组成部分,用来与虚拟机中的操作系统进行数据传输。virtio_add_queue函数在hw/virtio/virtio.c中,代码如下:

VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
                            VirtIOHandleOutput handle_output)
{
    int i;

    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        if (vdev->vq[i].vring.num == 0)
            break;
    }

    if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
        abort();

    vdev->vq[i].vring.num = queue_size;
    vdev->vq[i].vring.num_default = queue_size;
    vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
    vdev->vq[i].handle_output = handle_output;
    vdev->vq[i].used_elems = g_new0(VirtQueueElement, queue_size);

    return &vdev->vq[i];
}

virtio_add_queue函数是virtio框架中用来添加virtqueue的接口,其3个参数分别表示要添加的设备(VirtIODevice *vdev)、virtqueue的大小(int queue_size)以及处理函数(VirtIOHandleOutput handle_output)。

virtio_add_queue函数从VirtIODevice的vq数组成员中找到还未被使用的一个queue。一个virtqueue使用VirtQueue结构表示,这里对VirtQueue的成员进行初始化,包括这个queue的大小以及对齐等信息。最重要的是设置VirtQueue的handle_output成员,其是一个函数指针,在收到虚拟机发过来的IO请求时,会调用存放在handle_output中的回调函数。

VirtQueue结构的定义在hw/virtio/virtio.c中,如下:

struct VirtQueue
{
    VRing vring;
    VirtQueueElement *used_elems;

    /* Next head to pop */
    uint16_t last_avail_idx;
    bool last_avail_wrap_counter;

    /* Last avail_idx read from VQ. */
    uint16_t shadow_avail_idx;
    bool shadow_avail_wrap_counter;

    uint16_t used_idx;
    bool used_wrap_counter;

    /* Last used index value we have signalled on */
    uint16_t signalled_used;

    /* Last used index value we have signalled on */
    bool signalled_used_valid;

    /* Notification enabled? */
    bool notification;

    uint16_t queue_index;

    unsigned int inuse;

    uint16_t vector;
    VirtIOHandleOutput handle_output;
    VirtIODevice *vdev;
    EventNotifier guest_notifier;
    EventNotifier host_notifier;
    bool host_notifier_enabled;
    QLIST_ENTRY(VirtQueue) node;
};

欲知后事如何,且看下回分解。


http://www.niftyadmin.cn/n/5269298.html

相关文章

前后端开发鄙视链的真相,希望对从事前后端开发的小伙伴有些帮助

一、常规的工资对比 前后端的工资情况怎么样?过来人可以负责任的告诉大家:据我所知,至少在杭的网易、阿里,前端跟后端是一个批发价。

ububtu16.04下安装MQTT服务器

1、mqtt服务器安装 直接上root用户&#xff0c;顺序执行以下命令完成服务器安装&#xff1a; sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa sudo apt-get update sudo apt-get install mosquitto …

CSS新手入门笔记整理:CSS3选择器

属性选择器 属性选择器&#xff0c;指的是通过“元素的属性”来选择元素的一种方式。 语法 元素[attr^"xxx"]{} 元素[attr$"xxx"]{} 元素[attr*"xxx"]{} 选择器 说明 E[attr^"xxx"] 选择元素E&#xff0c;其中E元素的attr属性是…

STM8L151C8单片机学习例程(9)——Unique-ID

直接点击打不开&#xff0c;右键新建窗口打开链接 STM8L151C8单片机学习例程&#xff08;7&#xff09;——Unique-ID

简历提示:如何撰写出色的简历

您的简历可能是您一生中写的最重要的一页。遵循我们的 20 条简历写作技巧&#xff0c;让您的简历取得成功。 您知道一份出色的简历的重要性。这是您获得一份好工作所需的文件&#xff0c;而一份好工作可以带来美好的生活。因此&#xff0c;我们整理了 20 个简历技巧来帮助您撰…

Python学习之复习MySQL-Day6(约束)

目录 文章声明⭐⭐⭐让我们开始今天的学习吧&#xff01;约束简介概念目的分类 约束演示外键约束概念添加外键删除外键删除/更新行为 文章声明⭐⭐⭐ 该文章为我&#xff08;有编程语言基础&#xff0c;非编程小白&#xff09;的 MySQL复习笔记知识来源为 B站UP主&#xff08;…

JS中for循环之退出循环

我为大家介绍一下退出循环的两种方法 1.continue 退出本次循环&#xff0c;一般用于排除或者跳过某一个选项的时候&#xff0c;可以使用continue for(let i 0;i<5;i){if(i 3){continue}// 跳过了3console.log(i) //0 1 2 4}2.break 退出整个for循环&#xff0c;一般用于…

41、BatchNorm - 什么是批归一化

在 CNN 网络中有一个很重要的技术,叫作批归一化(bn, BatchNorm )。 归一化层一般位于卷积的后面,学术或者工程上,一般习惯将卷积+批归一化+激活统一成一个小的网络结构,比如口语化上称为conv+bn+relu。 这是因为基本上卷积后面肯定会有批归一化,而后面肯定会接激活函数…