[dpdk-dev] [RFC 03/24] vhost: allocate per-socket transport state
Stefan Hajnoczi
stefanha at redhat.com
Fri Jan 19 14:44:23 CET 2018
vhost-user transports have per-socket state (like file descriptors).
Make it possible for transports to keep state beyond what is included in
struct vhost_user_socket.
This patch makes it possible to move AF_UNIX-specific fields from struct
vhost_user_socket into trans_af_unix.c in later patches.
Signed-off-by: Stefan Hajnoczi <stefanha at redhat.com>
---
lib/librte_vhost/vhost.h | 9 +++++++++
lib/librte_vhost/socket.c | 6 ++++--
lib/librte_vhost/trans_af_unix.c | 5 +++++
3 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
index 8c6d6e524..e5279a572 100644
--- a/lib/librte_vhost/vhost.h
+++ b/lib/librte_vhost/vhost.h
@@ -210,6 +210,9 @@ struct virtio_net;
* A structure containing function pointers for transport-specific operations.
*/
struct vhost_transport_ops {
+ /** Size of struct vhost_user_socket-derived per-socket state */
+ size_t socket_size;
+
/**
* Notify the guest that used descriptors have been added to the vring.
* The VRING_AVAIL_F_NO_INTERRUPT flag has already been checked so this
@@ -273,6 +276,11 @@ TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
/*
* Every time rte_vhost_driver_register() is invoked, an associated
* vhost_user_socket struct will be created.
+ *
+ * Transport-specific per-socket state can be kept by embedding this struct at
+ * the beginning of a transport-specific struct. Set
+ * vhost_transport_ops->socket_size to the size of the transport-specific
+ * struct.
*/
struct vhost_user_socket {
struct vhost_user_connection_list conn_list;
@@ -296,6 +304,7 @@ struct vhost_user_socket {
uint64_t features;
struct vhost_device_ops const *notify_ops;
+ struct vhost_transport_ops const *trans_ops;
};
struct vhost_user_connection {
diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c
index d681f9cae..fffffc663 100644
--- a/lib/librte_vhost/socket.c
+++ b/lib/librte_vhost/socket.c
@@ -128,6 +128,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
{
int ret = -1;
struct vhost_user_socket *vsocket;
+ const struct vhost_transport_ops *trans_ops = &af_unix_trans_ops;
if (!path)
return -1;
@@ -140,10 +141,11 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
goto out;
}
- vsocket = malloc(sizeof(struct vhost_user_socket));
+ vsocket = malloc(trans_ops->socket_size);
if (!vsocket)
goto out;
- memset(vsocket, 0, sizeof(struct vhost_user_socket));
+ memset(vsocket, 0, trans_ops->socket_size);
+ vsocket->trans_ops = trans_ops;
vsocket->path = strdup(path);
if (vsocket->path == NULL) {
RTE_LOG(ERR, VHOST_CONFIG,
diff --git a/lib/librte_vhost/trans_af_unix.c b/lib/librte_vhost/trans_af_unix.c
index 636f69916..5e3c5ab2a 100644
--- a/lib/librte_vhost/trans_af_unix.c
+++ b/lib/librte_vhost/trans_af_unix.c
@@ -42,6 +42,10 @@
#define MAX_VIRTIO_BACKLOG 128
+struct af_unix_socket {
+ struct vhost_user_socket socket; /* must be the first field! */
+};
+
static void vhost_user_read_cb(int connfd, void *dat, int *remove);
/* return bytes# of read on success or negative val on failure. */
@@ -496,5 +500,6 @@ af_unix_vring_call(struct virtio_net *dev __rte_unused,
}
const struct vhost_transport_ops af_unix_trans_ops = {
+ .socket_size = sizeof(struct af_unix_socket),
.vring_call = af_unix_vring_call,
};
--
2.14.3
More information about the dev
mailing list