深入探究Ubuntunvme

发布时间:2023-05-23

随着云计算、大数据等新兴技术的快速发展,存储技术也不断更新迭代。NVMe(Non-Volatile Memory Express)是一种在PCIe总线上运行的新型存储接口协议,相比传统的SATA、SAS等存储接口协议,NVMe在CPU与存储介质之间加入了更快的通讯通道,能够提供更高的存储性能。而Ubuntunvme,则是基于Linux内核中NVMe驱动所提供的一种针对NVMe存储器的通用文件系统。本文将从以下几个方面对Ubuntunvme进行详细的阐述。

一、系统架构

Ubuntunvme采用了NVMe标准接口协议,并严格遵循离线编程模式,包括NVMe命令集、NVMe队列以及门铃模式。由于NVMe所支持的CMD和Doorbell已经向上提供了共享内存,NVMe设备直接映射共享内存来实现命令环和设备环之间的通讯。而在NVMe驱动内核上层,Ubuntunvme使用了通用文件系统,并支持两个主要函数:inode和dentry。

static const struct address_space_operations nvme_dax_aops = {
	.fault		= dax_noflush_fault,
	.writepage	= dax_writeback_mapping_range,
	.direct_io	= nvme_dax_direct_access,
};

在这段代码中,可以看到Ubuntunvme使用了通用文件系统,并定义了dax_aops用于操作address_space,同时设置了三个操作:fault、writepage和direct_io。这些操作函数都是NVMe所支持的。

二、特点和应用

Ubuntunvme与传统文件系统最大的不同,就是由NVMe命令集指定的直接读写,而不需要经过系统缓存区。这种基于直接读写的存储方式,可以大大提高系统I/O性能,且具备以下特点:

1、高性能

由于Ubuntunvme能够快速完成CMD和Doorbell之间的通讯,且不需要经过系统缓存区,因此能够大幅提升系统I/O性能。这对于需要大量数据读写操作的应用场景,如大数据、机器学习等领域具有重要意义。

2、支持大容量存储设备

相比传统的存储协议,NVMe协议支持更大的LBA寻址空间,能够支持16TB以上的存储设备。而Ubuntunvme也能够完美支持这些大容量存储设备,并保持较高的性能。

3、较低的主机CPU和内存负载

由于Ubuntunvme是直接操作存储设备的文件系统,因此相比随系统加载的传统文件系统,其启动时无需占用大量的内存空间,大大降低了系统的CPU和内存负载。

三、示例代码

以下是一个基于Ubuntunvme的示例程序,用于读写NVMe设备。示例代码采用C语言编写,并使用了DPDK库函数。

#include <rte_mbuf.h>
#include <rte_ethdev.h>
#include <rte_distributor.h>
#include <rte_ring.h>
#include <rte_malloc.h>
#include <rte_eal.h>
#define NUM_MBUFS 8191
#define MBUF_CACHE_SIZE 250
#define RX_RING_SIZE 1024
#define TX_RING_SIZE 1024
#define BURST_SIZE 32
static const struct rte_eth_conf port_conf_default = {
	.rxmode = {
		.max_rx_pkt_len = ETHER_MAX_LEN,
	},
};
static struct rte_eth_dev_tx_buffer *buffer;
struct rte_mempool *mbuf_pool;
static inline uint16_t
add_bonded_device(const char *devargs, uint8_t port_id,
		  uint32_t numa_node)
{
	struct rte_eth_conf port_conf = port_conf_default;
	struct rte_eth_dev_info dev_info;
	struct rte_eth_link link;
	uint16_t nb_queues = rte_eth_dev_count_avail();
	if (!nb_queues) {
		RTE_LOG(ERR, APP, "No ethdev ports available\n");
		return 0;
	}
	int ret = rte_eth_dev_configure(port_id, nb_queues, nb_queues, &port_conf);
	if (ret < 0) {
		RTE_LOG(ERR, APP,
			"Cannot configure device: "
			"err=%d, port=%d\n",
			ret, port_id);
		return 0;
	}
	rte_eth_dev_info_get(port_id, &dev_info);
	rte_eth_link_get(port_id, &link);
	struct rte_eth_rxconf rx_conf;
	struct rte_eth_txconf tx_conf;
	rx_conf = dev_info.default_rxconf;
	tx_conf = dev_info.default_txconf;
	tx_conf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
	uint16_t q;
	for (q = 0; q < nb_queues; q++) {
		ret = rte_eth_rx_queue_setup(port_id, q, RX_RING_SIZE,
					     numa_node, &rx_conf, mbuf_pool);
		if (ret < 0) {
			RTE_LOG(ERR, APP, "Cannot allocate rx queue: err=%d,"
				" port=%d\n", ret, port_id);
			return 0;
		}
		ret = rte_eth_tx_queue_setup(port_id, q, TX_RING_SIZE,
					      numa_node, &tx_conf);
		if (ret < 0) {
			RTE_LOG(ERR, APP, "Cannot allocate tx queue: err=%d,"
				" port=%d\n", ret, port_id);
			return 0;
		}
		buffer = rte_zmalloc_socket(NULL,
				RTE_ETH_TX_BUFFER_SIZE(BURST_SIZE), 0,
				rte_eth_dev_socket_id(port_id));
		if (buffer == NULL) {
			RTE_LOG(ERR, APP,
				"Cannot allocate buffer for tx on port %u\n",
				port_id);
			return 0;
		}
		ret = rte_eth_tx_buffer_init(buffer, BURST_SIZE);
		if (ret < 0) {
			RTE_LOG(ERR, APP,
				"Cannot init buffer for tx on port %u\n",
				port_id);
			return ret;
		}
	}
	return nb_queues;
}
static inline void
dpdk_init(void)
{
	int ret = rte_eal_init(rte_argv_count(rte_argv()), rte_argv());
	RTE_CHECK(ret >= 0, "Cannot init EAL\n");
}
int main(int argc, char **argv)
{
	dpdk_init();
	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
		NUM_MBUFS * rte_eth_dev_count_avail(),
		MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
	uint8_t port_id = 0;
	uint32_t numa_node = rte_socket_id();
	int nb_queues = add_bonded_device(NULL, port_id, numa_node);
	if (!nb_queues) {
		RTE_LOG(ERR, APP, "Cannot create bonded device\n");
		return 0;
	}
	printf("Hello, Ubuntunvme!\n");
	return 0;
}

这段代码主要用于创建DPDK虚拟网络设备并进行绑定,然后使用Ubuntunvme进行NVMe设备上的数据读写操作。其中,rte_eal_init用于初始化DPDK环境,rte_pktmbuf_pool_create用于创建DPDK内存池,add_bonded_device用于创建虚拟网络设备。这些操作都是Ubuntunvme的API,能够方便地对NVMe设备进行操作。