概括來(lái)說(shuō)帜乞,qemu和KVM在內(nèi)存管理上的關(guān)系就是:在虛擬機(jī)啟動(dòng)時(shí)嗽测,qemu在qemu進(jìn)程地址空間申請(qǐng)內(nèi)存撑帖,即內(nèi)存的申請(qǐng)是在用戶空間完成的蓉坎。通過(guò)kvm提供的API,把地址信息注冊(cè)到KVM中胡嘿,這樣KVM中維護(hù)有虛擬機(jī)相關(guān)的slot蛉艾,這些slot構(gòu)成了一個(gè)完整的虛擬機(jī)物理地址空間。slot中記錄了其對(duì)應(yīng)的HVA衷敌,頁(yè)面數(shù)勿侯、起始GPA等,利用它可以把一個(gè)GPA轉(zhuǎn)化成HVA缴罗,這正是KVM維護(hù)EPT的技術(shù)核心助琐。整個(gè)內(nèi)存虛擬化可以分為兩部分:qemu部分和kvm部分。qemu完成內(nèi)存的申請(qǐng)面氓,kvm實(shí)現(xiàn)內(nèi)存的管理兵钮。
- qemu中地址空間分兩部分,兩個(gè)全局變量system_memory和system_IO侧但,其中system_memory是所有memory_region的父object矢空,他們只負(fù)責(zé)管理內(nèi)存。
- 在KVM中禀横,也有兩個(gè)全局變量address_space_memory和address_space_memory_IO屁药,與qemu中的memory_region對(duì)應(yīng),只有將HVA和GPA的對(duì)應(yīng)關(guān)系注冊(cè)到KVM模塊的memslot柏锄,才可以生效成為EPT酿箭。
在qemu 2.9的前端virtio和dpdk17.05的后端vhost-user構(gòu)成的虛擬隊(duì)列中复亏,會(huì)率先通過(guò)socket建立連接,將qemu中virtio的內(nèi)存布局傳給vhost缭嫡,vhost收到包(該消息機(jī)制有自己的協(xié)議缔御,暫稱(chēng)為msg)后,分析其中的信息妇蛀,這里面通信包含一套自己寫(xiě)的協(xié)議耕突。包含以下內(nèi)容,均是在剛建立連接時(shí)候傳遞的:
static const char *vhost_message_str[VHOST_USER_MAX] = {
[VHOST_USER_NONE] = "VHOST_USER_NONE",
[VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
[VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
[VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
[VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
[VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
[VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
[VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
[VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
[VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
[VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
[VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
[VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
[VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
[VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
[VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
[VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
[VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
[VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
[VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
[VHOST_USER_NET_SET_MTU] = "VHOST_USER_NET_SET_MTU",
};
其中我們最關(guān)心的就是vhost_user_set_mem_table:
static int
vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
{
...
for (i = 0; i < memory.nregions; i++) {
fd = pmsg->fds[i];
reg = &dev->mem->regions[i];
reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
reg->guest_user_addr = memory.regions[i].userspace_addr;
reg->size = memory.regions[i].memory_size;
reg->fd = fd;
mmap_offset = memory.regions[i].mmap_offset;
mmap_size = reg->size + mmap_offset;
/* mmap() without flag of MAP_ANONYMOUS, should be called
* with length argument aligned with hugepagesz at older
* longterm version Linux, like 2.6.32 and 3.2.72, or
* mmap() will fail with EINVAL.
*
* to avoid failure, make sure in caller to keep length
* aligned.
*/
alignment = get_blk_size(fd);
if (alignment == (uint64_t)-1) {
RTE_LOG(ERR, VHOST_CONFIG,
"couldn't get hugepage size through fstat\n");
goto err_mmap;
}
mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, 0);
//對(duì)每個(gè)region調(diào)用mmap映射共享內(nèi)存
if (mmap_addr == MAP_FAILED) {
RTE_LOG(ERR, VHOST_CONFIG,
"mmap region %u failed.\n", i);
goto err_mmap;
}
...
return 0;
err_mmap:
free_mem_region(dev);
rte_free(dev->mem);
dev->mem = NULL;
return -1;
}
另外评架,我們?cè)趯?shí)際運(yùn)行系統(tǒng)的過(guò)程中發(fā)現(xiàn)眷茁,qemu的內(nèi)存布局和vhost端的內(nèi)存布局,雖是通過(guò)共享內(nèi)存建立的纵诞,但是既不是一整塊內(nèi)存映射上祈,也不是通過(guò)零碎的region一小塊一小塊的映射。它們的內(nèi)存布局如下:
在vhost這邊只有兩塊region浙芙,而且像是將前端的內(nèi)存region做了一個(gè)聚合得到的登刺。回歸代碼嗡呼,發(fā)現(xiàn)消息傳遞之前纸俭,傳遞的并非是memory_region變量,而是memory_region_section晤锥,在qemu的vhost_set_memory函數(shù)中掉蔬,有這樣一個(gè)操作:
if (add) {
/* Add given mapping, merging adjacent regions if any */
vhost_dev_assign_memory(dev, start_addr, size, (uintptr_t)ram);
} else {
/* Remove old mapping for this memory, if any. */
vhost_dev_unassign_memory(dev, start_addr, size);
}
將毗鄰的memory_region合并了,這樣就解釋的通了矾瘾。因?yàn)閙emory_region是一個(gè)樹(shù)狀結(jié)構(gòu)女轿,且有包含關(guān)系在里面,所以如果一個(gè)個(gè)傳遞壕翩,vhost里面用for循環(huán)進(jìn)行映射到自己地址空間蛉迹,效率低下,而且大多數(shù)內(nèi)存vhost用不到放妈,沒(méi)有必要這么細(xì)分北救。