From patchwork Mon Nov 17 06:07:18 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tetsuya Mukawa X-Patchwork-Id: 1319 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 68DE07FC0; Mon, 17 Nov 2014 06:57:21 +0100 (CET) Received: from mail-pa0-f46.google.com (mail-pa0-f46.google.com [209.85.220.46]) by dpdk.org (Postfix) with ESMTP id A2A737FA1 for ; Mon, 17 Nov 2014 06:57:15 +0100 (CET) Received: by mail-pa0-f46.google.com with SMTP id lf10so21608771pab.33 for ; Sun, 16 Nov 2014 22:07:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=EVtPB80pTQe/yf/aWSmCqvGqgS1Awn4bkQvBpfS61b8=; b=ZAj4H8KSB3zvUn1WtKNk9H6INDn+sXJ5L5XcRTBxLd2n6I3Kg4ha9DvtAJeuvhEWPJ sje3Hev0n02ZpLWQB793aMRlw/J1w1G+4eTegb7GAVMhGVcyzqRSYv28014+i40JgJmc RBvx1emUzxGMrQxD+aT1mfpBitG0MNNpFQzZx87ql/U1vLHP7E1ZH087QhZJ1+9mnP5h 7MDeeIIAYvWuBVm1tK76483na2R0/iUysBfKtt2sOdAtuWIM7WFkpnIr5lmBwF+G5VWZ PtgTwNXWNFQMQn/nt5fyFGcExw2FVbwUJSE+G2dEJbjb6BSDaPSmC16zourJ7FrH7BPW FQOg== X-Gm-Message-State: ALoCoQmdclsgqd9RGj+kIyTAmLwzPNh8n4ASUbNMBziVsVwuThyGuB/EHv6ExLMCu04LALOip32S X-Received: by 10.66.188.200 with SMTP id gc8mr27607838pac.38.1416204450480; Sun, 16 Nov 2014 22:07:30 -0800 (PST) Received: from localhost.localdomain (napt.igel.co.jp. [219.106.231.132]) by mx.google.com with ESMTPSA id oe10sm33953028pdb.66.2014.11.16.22.07.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 16 Nov 2014 22:07:29 -0800 (PST) From: Tetsuya Mukawa To: dev@dpdk.org Date: Mon, 17 Nov 2014 15:07:18 +0900 Message-Id: <1416204438-4327-2-git-send-email-mukawa@igel.co.jp> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1416204438-4327-1-git-send-email-mukawa@igel.co.jp> References: <1416014087-22499-1-git-send-email-huawei.xie@intel.com> <1416204438-4327-1-git-send-email-mukawa@igel.co.jp> Subject: [dpdk-dev] [RFC PATCH 2/2] lib/librte_vhost: Add device abstraction layer X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" --- lib/librte_vhost/Makefile | 6 +- lib/librte_vhost/rte_virtio_net.h | 22 ++++- lib/librte_vhost/vhost-cuse/vhost-net-cdev.c | 6 +- lib/librte_vhost/vhost-cuse/vhost-net-cdev.h | 40 +++++++++ lib/librte_vhost/vhost-cuse/virtio-net-cdev.c | 1 + lib/librte_vhost/vhost-net.c | 101 +++++++++++++++++++++++ lib/librte_vhost/vhost-net.h | 114 ++++++++++++++++++++++++++ lib/librte_vhost/vhost-user/vhost-net-user.c | 6 +- lib/librte_vhost/vhost-user/vhost-net-user.h | 3 + lib/librte_vhost/vhost-user/virtio-net-user.c | 1 + 10 files changed, 290 insertions(+), 10 deletions(-) create mode 100644 lib/librte_vhost/vhost-cuse/vhost-net-cdev.h create mode 100644 lib/librte_vhost/vhost-net.c create mode 100644 lib/librte_vhost/vhost-net.h diff --git a/lib/librte_vhost/Makefile b/lib/librte_vhost/Makefile index cb4e172..4363a14 100644 --- a/lib/librte_vhost/Makefile +++ b/lib/librte_vhost/Makefile @@ -37,11 +37,11 @@ LIB = librte_vhost.a CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -I. -I vhost-user -I vhost-cuse -O3 -D_FILE_OFFSET_BITS=64 -lfuse LDFLAGS += -lfuse # all source are stored in SRCS-y -#SRCS-$(CONFIG_RTE_LIBRTE_VHOST) := vhost-cuse/vhost-net-cdev.c vhost-cuse/virtio-net-cdev.c +SRCS-$(CONFIG_RTE_LIBRTE_VHOST) := vhost-cuse/vhost-net-cdev.c vhost-cuse/virtio-net-cdev.c -SRCS-$(CONFIG_RTE_LIBRTE_VHOST) := vhost-user/fd_man.c vhost-user/vhost-net-user.c vhost-user/virtio-net-user.c +SRCS-$(CONFIG_RTE_LIBRTE_VHOST) += vhost-user/fd_man.c vhost-user/vhost-net-user.c vhost-user/virtio-net-user.c -SRCS-$(CONFIG_RTE_LIBRTE_VHOST) += virtio-net.c vhost_rxtx.c +SRCS-$(CONFIG_RTE_LIBRTE_VHOST) += virtio-net.c vhost_rxtx.c vhost-net.c # install includes SYMLINK-$(CONFIG_RTE_LIBRTE_VHOST)-include += rte_virtio_net.h diff --git a/lib/librte_vhost/rte_virtio_net.h b/lib/librte_vhost/rte_virtio_net.h index a09533d..116c7e9 100644 --- a/lib/librte_vhost/rte_virtio_net.h +++ b/lib/librte_vhost/rte_virtio_net.h @@ -140,6 +140,23 @@ gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa) } /** + * Enum for vhost driver types. + */ +enum rte_vhost_driver_t { + VHOST_DRV_CUSE, /* vhost-cuse driver */ + VHOST_DRV_USER, /* vhost-user driver */ + VHOST_DRV_NUM /* the number of vhost driver types */ +}; + +/** + * Structure contains information relating vhost driver. + */ +struct rte_vhost_driver { + enum rte_vhost_driver_t type; /**< driver type. */ + const char *dev_name; /**< accessing device name. */ +}; + +/** * Disable features in feature_mask. Returns 0 on success. */ int rte_vhost_feature_disable(uint64_t feature_mask); @@ -155,12 +172,13 @@ uint64_t rte_vhost_feature_get(void); int rte_vhost_enable_guest_notification(struct virtio_net *dev, uint16_t queue_id, int enable); /* Register vhost driver. dev_name could be different for multiple instance support. */ -int rte_vhost_driver_register(const char *dev_name); +struct rte_vhost_driver *rte_vhost_driver_register( + const char *dev_name, enum rte_vhost_driver_t type); /* Register callbacks. */ int rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const); -int rte_vhost_driver_session_start(void); +int rte_vhost_driver_session_start(struct rte_vhost_driver *drv); /** * This function adds buffers to the virtio devices RX virtqueue. Buffers can diff --git a/lib/librte_vhost/vhost-cuse/vhost-net-cdev.c b/lib/librte_vhost/vhost-cuse/vhost-net-cdev.c index 688ec00..6ea54ee 100644 --- a/lib/librte_vhost/vhost-cuse/vhost-net-cdev.c +++ b/lib/librte_vhost/vhost-cuse/vhost-net-cdev.c @@ -47,6 +47,7 @@ #include "virtio-net-cdev.h" #include "vhost-net.h" +#include "vhost-net-cdev.h" #include "eventfd_link/eventfd_link.h" #define FUSE_OPT_DUMMY "\0\0" @@ -373,8 +374,9 @@ static const struct cuse_lowlevel_ops vhost_net_ops = { * vhost_net_device_ops are also passed when the device is registered in app. */ int -rte_vhost_driver_register(const char *dev_name) +vhost_cuse_driver_register(struct rte_vhost_driver *drv) { + const char *dev_name = drv->dev_name; struct cuse_info cuse_info; char device_name[PATH_MAX] = ""; char char_device_name[PATH_MAX] = ""; @@ -428,7 +430,7 @@ rte_vhost_driver_register(const char *dev_name) * release and ioctl calls. */ int -rte_vhost_driver_session_start(void) +vhost_cuse_driver_session_start(void) { fuse_session_loop(session); diff --git a/lib/librte_vhost/vhost-cuse/vhost-net-cdev.h b/lib/librte_vhost/vhost-cuse/vhost-net-cdev.h new file mode 100644 index 0000000..cb094ee --- /dev/null +++ b/lib/librte_vhost/vhost-cuse/vhost-net-cdev.h @@ -0,0 +1,40 @@ +/*- + * BSD LICENSE + * + * Copyright(c) 2014 IGEL Co.,Ltd. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of IGEL nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _VHOST_NET_CDEV_H +#define _VHOST_NET_CDEV_H + +int vhost_cuse_driver_register(struct rte_vhost_driver *drv); +int vhost_cuse_driver_session_start(void); + +#endif diff --git a/lib/librte_vhost/vhost-cuse/virtio-net-cdev.c b/lib/librte_vhost/vhost-cuse/virtio-net-cdev.c index 7381140..42a6b24 100644 --- a/lib/librte_vhost/vhost-cuse/virtio-net-cdev.c +++ b/lib/librte_vhost/vhost-cuse/virtio-net-cdev.c @@ -46,6 +46,7 @@ #include #include +#include #include "vhost-net.h" #include "virtio-net-cdev.h" diff --git a/lib/librte_vhost/vhost-net.c b/lib/librte_vhost/vhost-net.c new file mode 100644 index 0000000..7a4537d --- /dev/null +++ b/lib/librte_vhost/vhost-net.c @@ -0,0 +1,101 @@ +/*- + * BSD LICENSE + * + * Copyright(c) 2014 IGEL Co.,Ltd. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of IGEL nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include "vhost-cuse/vhost-net-cdev.h" +#include "vhost-user/vhost-net-user.h" + +/** + * This function abstracts cuse and vhost-user driver registration. + */ +struct rte_vhost_driver * +rte_vhost_driver_register(const char *dev_name, enum rte_vhost_driver_t type) +{ + int ret; + struct rte_vhost_driver *drv; + + drv = rte_zmalloc(dev_name, sizeof(struct rte_vhost_driver), + CACHE_LINE_SIZE); + if (drv == NULL) + return NULL; + + drv->dev_name = dev_name; + drv->type = type; + + switch (type) { + case VHOST_DRV_CUSE: + ret = vhost_cuse_driver_register(drv); + if (ret != 0) + goto err; + break; + case VHOST_DRV_USER: + ret = vhost_user_driver_register(drv); + if (ret != 0) + goto err; + break; + default: + break; + } + + return drv; +err: + free(drv); + return NULL; +} + +/** + * The session is launched allowing the application to + * receive open, release and ioctl calls. + */ +int +rte_vhost_driver_session_start(struct rte_vhost_driver *drv) +{ + if (drv == NULL) + return -ENODEV; + + switch (drv->type) { + case VHOST_DRV_CUSE: + vhost_cuse_driver_session_start(); + break; + case VHOST_DRV_USER: + vhost_user_driver_session_start(); + break; + default: + break; + } + + return 0; +} diff --git a/lib/librte_vhost/vhost-net.h b/lib/librte_vhost/vhost-net.h new file mode 100644 index 0000000..881a45f --- /dev/null +++ b/lib/librte_vhost/vhost-net.h @@ -0,0 +1,114 @@ +/*- + * BSD LICENSE + * + * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _VHOST_NET_H_ +#define _VHOST_NET_H_ +#include +#include +#include +#include +#include + +#include + +/* Macros for printing using RTE_LOG */ +#define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1 +#define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1 + +#ifdef RTE_LIBRTE_VHOST_DEBUG +#define VHOST_MAX_PRINT_BUFF 6072 +#define LOG_LEVEL RTE_LOG_DEBUG +#define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args) +#define VHOST_PRINT_PACKET(device, addr, size, header) do { \ + char *pkt_addr = (char *)(addr); \ + unsigned int index; \ + char packet[VHOST_MAX_PRINT_BUFF]; \ + \ + if ((header)) \ + snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%"PRIu64") Header size %d: ", (device->device_fh), (size)); \ + else \ + snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%"PRIu64") Packet size %d: ", (device->device_fh), (size)); \ + for (index = 0; index < (size); index++) { \ + snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \ + "%02hhx ", pkt_addr[index]); \ + } \ + snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \ + \ + LOG_DEBUG(VHOST_DATA, "%s", packet); \ +} while (0) +#else +#define LOG_LEVEL RTE_LOG_INFO +#define LOG_DEBUG(log_type, fmt, args...) do {} while (0) +#define VHOST_PRINT_PACKET(device, addr, size, header) do {} while (0) +#endif + + +/* + * Structure used to identify device context. + */ +struct vhost_device_ctx { + pid_t pid; /* PID of process calling the IOCTL. */ + uint64_t fh; /* Populated with fi->fh to track the device index. */ +}; + +/* + * Structure contains function pointers to be defined in virtio-net.c. These + * functions are called in CUSE context and are used to configure devices. + */ +struct vhost_net_device_ops { + int (*new_device)(struct vhost_device_ctx); + void (*destroy_device)(struct vhost_device_ctx); + + int (*get_features)(struct vhost_device_ctx, uint64_t *); + int (*set_features)(struct vhost_device_ctx, uint64_t *); + + int (*set_mem_table)(struct vhost_device_ctx, + const struct virtio_memory_regions *, uint32_t); + + int (*set_vring_num)(struct vhost_device_ctx, struct vhost_vring_state *); + int (*set_vring_addr)(struct vhost_device_ctx, struct vhost_vring_addr *); + int (*set_vring_base)(struct vhost_device_ctx, struct vhost_vring_state *); + int (*get_vring_base)(struct vhost_device_ctx, uint32_t, struct vhost_vring_state *); + + int (*set_vring_kick)(struct vhost_device_ctx, struct vhost_vring_file *); + int (*set_vring_call)(struct vhost_device_ctx, struct vhost_vring_file *); + + int (*set_backend)(struct vhost_device_ctx, struct vhost_vring_file *); + + int (*set_owner)(struct vhost_device_ctx); + int (*reset_owner)(struct vhost_device_ctx); +}; + + +struct vhost_net_device_ops const *get_virtio_net_callbacks(void); +#endif /* _VHOST_NET_H_ */ diff --git a/lib/librte_vhost/vhost-user/vhost-net-user.c b/lib/librte_vhost/vhost-user/vhost-net-user.c index 0b100ba..837f840 100644 --- a/lib/librte_vhost/vhost-user/vhost-net-user.c +++ b/lib/librte_vhost/vhost-user/vhost-net-user.c @@ -371,9 +371,9 @@ vserver_message_handler(int connfd, uint64_t dat) * Creates and initialise the vhost server. */ int -rte_vhost_driver_register(const char *path) +vhost_user_driver_register(struct rte_vhost_driver *drv) { - + const char *path = drv->dev_name; struct vhost_server *vserver; if (g_vhost_server != NULL) @@ -408,7 +408,7 @@ rte_vhost_driver_register(const char *path) int -rte_vhost_driver_session_start(void) +vhost_user_driver_session_start(void) { fdset_event_dispatch(&g_vhost_server->fdset); return 0; diff --git a/lib/librte_vhost/vhost-user/vhost-net-user.h b/lib/librte_vhost/vhost-user/vhost-net-user.h index c9df9fa..d90c147 100644 --- a/lib/librte_vhost/vhost-user/vhost-net-user.h +++ b/lib/librte_vhost/vhost-user/vhost-net-user.h @@ -71,4 +71,7 @@ typedef struct VhostUserMsg { #define VHOST_USER_VERSION (0x1) /*****************************************************************************/ +int vhost_user_driver_register(struct rte_vhost_driver *drv); +int vhost_user_driver_session_start(void); + #endif diff --git a/lib/librte_vhost/vhost-user/virtio-net-user.c b/lib/librte_vhost/vhost-user/virtio-net-user.c index 4103977..f839219 100644 --- a/lib/librte_vhost/vhost-user/virtio-net-user.c +++ b/lib/librte_vhost/vhost-user/virtio-net-user.c @@ -38,6 +38,7 @@ #include #include +#include #include "virtio-net-user.h" #include "vhost-net-user.h"