From: Rich Lane Date: Fri, 19 Feb 2016 18:10:16 +0000 (-0800) Subject: vhost: remove device operations pointers X-Git-Tag: spdx-start~7606 X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=a90ca1a12eb560f0ca8e76a6b116cd1fc9ac0439;p=dpdk.git vhost: remove device operations pointers The vhost_net_device_ops indirection is unnecessary because there is only one implementation of the vhost common code. Removing it makes the code more readable. Signed-off-by: Rich Lane Acked-by: Yuanhan Liu --- diff --git a/examples/vhost_xen/virtio-net.h b/examples/vhost_xen/virtio-net.h index c8c5a7a3e1..ab697260c9 100644 --- a/examples/vhost_xen/virtio-net.h +++ b/examples/vhost_xen/virtio-net.h @@ -110,6 +110,4 @@ struct virtio_net_device_ops { void (* destroy_device) (volatile struct virtio_net *); /* Remove device. */ }; -struct vhost_net_device_ops const * get_virtio_net_callbacks(void); - #endif diff --git a/lib/librte_vhost/vhost-net.h b/lib/librte_vhost/vhost-net.h index affbd1a497..f193a1f652 100644 --- a/lib/librte_vhost/vhost-net.h +++ b/lib/librte_vhost/vhost-net.h @@ -43,8 +43,6 @@ #include "rte_virtio_net.h" -extern struct vhost_net_device_ops const *ops; - /* Macros for printing using RTE_LOG */ #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1 @@ -85,36 +83,28 @@ struct vhost_device_ctx { 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); - - void (*set_ifname)(struct vhost_device_ctx, - const char *if_name, unsigned int if_len); - - int (*get_features)(struct vhost_device_ctx, uint64_t *); - int (*set_features)(struct vhost_device_ctx, uint64_t *); +int vhost_new_device(struct vhost_device_ctx); +void vhost_destroy_device(struct vhost_device_ctx); - 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 *); +void vhost_set_ifname(struct vhost_device_ctx, + const char *if_name, unsigned int if_len); - 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 vhost_get_features(struct vhost_device_ctx, uint64_t *); +int vhost_set_features(struct vhost_device_ctx, uint64_t *); - int (*set_backend)(struct vhost_device_ctx, struct vhost_vring_file *); +int vhost_set_vring_num(struct vhost_device_ctx, struct vhost_vring_state *); +int vhost_set_vring_addr(struct vhost_device_ctx, struct vhost_vring_addr *); +int vhost_set_vring_base(struct vhost_device_ctx, struct vhost_vring_state *); +int vhost_get_vring_base(struct vhost_device_ctx, + uint32_t, struct vhost_vring_state *); - int (*set_owner)(struct vhost_device_ctx); - int (*reset_owner)(struct vhost_device_ctx); -}; +int vhost_set_vring_kick(struct vhost_device_ctx, struct vhost_vring_file *); +int vhost_set_vring_call(struct vhost_device_ctx, struct vhost_vring_file *); +int vhost_set_backend(struct vhost_device_ctx, struct vhost_vring_file *); -struct vhost_net_device_ops const *get_virtio_net_callbacks(void); +int vhost_set_owner(struct vhost_device_ctx); +int vhost_reset_owner(struct vhost_device_ctx); /* * Backend-specific cleanup. Defined by vhost-cuse and vhost-user. diff --git a/lib/librte_vhost/vhost_cuse/vhost-net-cdev.c b/lib/librte_vhost/vhost_cuse/vhost-net-cdev.c index ae7ad8df7f..c613e68e8e 100644 --- a/lib/librte_vhost/vhost_cuse/vhost-net-cdev.c +++ b/lib/librte_vhost/vhost_cuse/vhost-net-cdev.c @@ -58,7 +58,6 @@ static const char cuse_device_name[] = "/dev/cuse"; static const char default_cdev[] = "vhost-net"; static struct fuse_session *session; -struct vhost_net_device_ops const *ops; /* * Returns vhost_device_ctx from given fuse_req_t. The index is populated later @@ -86,7 +85,7 @@ vhost_net_open(fuse_req_t req, struct fuse_file_info *fi) struct vhost_device_ctx ctx = fuse_req_to_vhost_ctx(req, fi); int err = 0; - err = ops->new_device(ctx); + err = vhost_new_device(ctx); if (err == -1) { fuse_reply_err(req, EPERM); return; @@ -108,7 +107,7 @@ vhost_net_release(fuse_req_t req, struct fuse_file_info *fi) int err = 0; struct vhost_device_ctx ctx = fuse_req_to_vhost_ctx(req, fi); - ops->destroy_device(ctx); + vhost_destroy_device(ctx); RTE_LOG(INFO, VHOST_CONFIG, "(%"PRIu64") Device released\n", ctx.fh); fuse_reply_err(req, err); } @@ -208,25 +207,25 @@ vhost_net_ioctl(fuse_req_t req, int cmd, void *arg, case VHOST_GET_FEATURES: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_GET_FEATURES\n", ctx.fh); - VHOST_IOCTL_W(uint64_t, features, ops->get_features); + VHOST_IOCTL_W(uint64_t, features, vhost_get_features); break; case VHOST_SET_FEATURES: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_FEATURES\n", ctx.fh); - VHOST_IOCTL_R(uint64_t, features, ops->set_features); + VHOST_IOCTL_R(uint64_t, features, vhost_set_features); break; case VHOST_RESET_OWNER: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_RESET_OWNER\n", ctx.fh); - VHOST_IOCTL(ops->reset_owner); + VHOST_IOCTL(vhost_reset_owner); break; case VHOST_SET_OWNER: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_OWNER\n", ctx.fh); - VHOST_IOCTL(ops->set_owner); + VHOST_IOCTL(vhost_set_owner); break; case VHOST_SET_MEM_TABLE: @@ -267,28 +266,28 @@ vhost_net_ioctl(fuse_req_t req, int cmd, void *arg, LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_VRING_NUM\n", ctx.fh); VHOST_IOCTL_R(struct vhost_vring_state, state, - ops->set_vring_num); + vhost_set_vring_num); break; case VHOST_SET_VRING_BASE: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_VRING_BASE\n", ctx.fh); VHOST_IOCTL_R(struct vhost_vring_state, state, - ops->set_vring_base); + vhost_set_vring_base); break; case VHOST_GET_VRING_BASE: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_GET_VRING_BASE\n", ctx.fh); VHOST_IOCTL_RW(uint32_t, index, - struct vhost_vring_state, state, ops->get_vring_base); + struct vhost_vring_state, state, vhost_get_vring_base); break; case VHOST_SET_VRING_ADDR: LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") IOCTL: VHOST_SET_VRING_ADDR\n", ctx.fh); VHOST_IOCTL_R(struct vhost_vring_addr, addr, - ops->set_vring_addr); + vhost_set_vring_addr); break; case VHOST_SET_VRING_KICK: @@ -316,10 +315,10 @@ vhost_net_ioctl(fuse_req_t req, int cmd, void *arg, } file.fd = fd; if (cmd == VHOST_SET_VRING_KICK) { - result = ops->set_vring_kick(ctx, &file); + result = vhost_set_vring_kick(ctx, &file); fuse_reply_ioctl(req, result, NULL, 0); } else { - result = ops->set_vring_call(ctx, &file); + result = vhost_set_vring_call(ctx, &file); fuse_reply_ioctl(req, result, NULL, 0); } } @@ -397,8 +396,6 @@ rte_vhost_driver_register(const char *dev_name) cuse_info.dev_info_argv = device_argv; cuse_info.flags = CUSE_UNRESTRICTED_IOCTL; - ops = get_virtio_net_callbacks(); - session = cuse_lowlevel_setup(3, fuse_argv, &cuse_info, &vhost_net_ops, 0, NULL); if (session == NULL) diff --git a/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c b/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c index 374c8846bb..a68a8bd446 100644 --- a/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c +++ b/lib/librte_vhost/vhost_cuse/virtio-net-cdev.c @@ -399,7 +399,7 @@ get_ifname(struct vhost_device_ctx ctx, struct virtio_net *dev, int tap_fd, int if (ret >= 0) { ifr_size = strnlen(ifr.ifr_name, sizeof(ifr.ifr_name)); - ops->set_ifname(ctx, ifr.ifr_name, ifr_size); + vhost_set_ifname(ctx, ifr.ifr_name, ifr_size); } else RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") TUNGETIFF ioctl failed\n", @@ -419,7 +419,7 @@ int cuse_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file) if (!(dev->flags & VIRTIO_DEV_RUNNING) && file->fd != VIRTIO_DEV_STOPPED) get_ifname(ctx, dev, file->fd, ctx.pid); - return ops->set_backend(ctx, file); + return vhost_set_backend(ctx, file); } void diff --git a/lib/librte_vhost/vhost_user/vhost-net-user.c b/lib/librte_vhost/vhost_user/vhost-net-user.c index 6ed76693fb..de7eecbbad 100644 --- a/lib/librte_vhost/vhost_user/vhost-net-user.c +++ b/lib/librte_vhost/vhost_user/vhost-net-user.c @@ -55,7 +55,6 @@ static void vserver_new_vq_conn(int fd, void *data, int *remove); static void vserver_message_handler(int fd, void *dat, int *remove); -struct vhost_net_device_ops const *ops; struct connfd_ctx { struct vhost_server *vserver; @@ -302,7 +301,7 @@ vserver_new_vq_conn(int fd, void *dat, __rte_unused int *remove) return; } - fh = ops->new_device(vdev_ctx); + fh = vhost_new_device(vdev_ctx); if (fh == -1) { free(ctx); close(conn_fd); @@ -311,7 +310,7 @@ vserver_new_vq_conn(int fd, void *dat, __rte_unused int *remove) vdev_ctx.fh = fh; size = strnlen(vserver->path, PATH_MAX); - ops->set_ifname(vdev_ctx, vserver->path, + vhost_set_ifname(vdev_ctx, vserver->path, size); RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh); @@ -348,7 +347,7 @@ vserver_message_handler(int connfd, void *dat, int *remove) close(connfd); *remove = 1; free(cfd_ctx); - ops->destroy_device(ctx); + vhost_destroy_device(ctx); return; } @@ -357,14 +356,14 @@ vserver_message_handler(int connfd, void *dat, int *remove) vhost_message_str[msg.request]); switch (msg.request) { case VHOST_USER_GET_FEATURES: - ret = ops->get_features(ctx, &features); + ret = vhost_get_features(ctx, &features); msg.payload.u64 = features; msg.size = sizeof(msg.payload.u64); send_vhost_message(connfd, &msg); break; case VHOST_USER_SET_FEATURES: features = msg.payload.u64; - ops->set_features(ctx, &features); + vhost_set_features(ctx, &features); break; case VHOST_USER_GET_PROTOCOL_FEATURES: @@ -377,10 +376,10 @@ vserver_message_handler(int connfd, void *dat, int *remove) break; case VHOST_USER_SET_OWNER: - ops->set_owner(ctx); + vhost_set_owner(ctx); break; case VHOST_USER_RESET_OWNER: - ops->reset_owner(ctx); + vhost_reset_owner(ctx); break; case VHOST_USER_SET_MEM_TABLE: @@ -400,13 +399,13 @@ vserver_message_handler(int connfd, void *dat, int *remove) break; case VHOST_USER_SET_VRING_NUM: - ops->set_vring_num(ctx, &msg.payload.state); + vhost_set_vring_num(ctx, &msg.payload.state); break; case VHOST_USER_SET_VRING_ADDR: - ops->set_vring_addr(ctx, &msg.payload.addr); + vhost_set_vring_addr(ctx, &msg.payload.addr); break; case VHOST_USER_SET_VRING_BASE: - ops->set_vring_base(ctx, &msg.payload.state); + vhost_set_vring_base(ctx, &msg.payload.state); break; case VHOST_USER_GET_VRING_BASE: @@ -456,8 +455,6 @@ rte_vhost_driver_register(const char *path) struct vhost_server *vserver; pthread_mutex_lock(&g_vhost_server.server_mutex); - if (ops == NULL) - ops = get_virtio_net_callbacks(); if (g_vhost_server.vserver_cnt == MAX_VHOST_SERVER) { RTE_LOG(ERR, VHOST_CONFIG, diff --git a/lib/librte_vhost/vhost_user/virtio-net-user.c b/lib/librte_vhost/vhost_user/virtio-net-user.c index ffce0d6295..68b24f45cb 100644 --- a/lib/librte_vhost/vhost_user/virtio-net-user.c +++ b/lib/librte_vhost/vhost_user/virtio-net-user.c @@ -269,7 +269,7 @@ user_set_vring_call(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg) file.fd = pmsg->fds[0]; RTE_LOG(INFO, VHOST_CONFIG, "vring call idx:%d file:%d\n", file.index, file.fd); - ops->set_vring_call(ctx, &file); + vhost_set_vring_call(ctx, &file); } @@ -290,7 +290,7 @@ user_set_vring_kick(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg) file.fd = pmsg->fds[0]; RTE_LOG(INFO, VHOST_CONFIG, "vring kick idx:%d file:%d\n", file.index, file.fd); - ops->set_vring_kick(ctx, &file); + vhost_set_vring_kick(ctx, &file); if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) @@ -313,7 +313,7 @@ user_get_vring_base(struct vhost_device_ctx ctx, notify_ops->destroy_device(dev); /* Here we are safe to get the last used index */ - ops->get_vring_base(ctx, state->index, state); + vhost_get_vring_base(ctx, state->index, state); RTE_LOG(INFO, VHOST_CONFIG, "vring base idx:%d file:%d\n", state->index, state->num); diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c index 196e1cf51e..e189c74375 100644 --- a/lib/librte_vhost/virtio-net.c +++ b/lib/librte_vhost/virtio-net.c @@ -350,8 +350,8 @@ reset_device(struct virtio_net *dev) * initialised and a new entry is added to the device configuration linked * list. */ -static int -new_device(struct vhost_device_ctx ctx) +int +vhost_new_device(struct vhost_device_ctx ctx) { struct virtio_net_config_ll *new_ll_dev; @@ -376,8 +376,8 @@ new_device(struct vhost_device_ctx ctx) * Function is called from the CUSE release function. This function will * cleanup the device and remove it from device configuration linked list. */ -static void -destroy_device(struct vhost_device_ctx ctx) +void +vhost_destroy_device(struct vhost_device_ctx ctx) { struct virtio_net_config_ll *ll_dev_cur_ctx, *ll_dev_last = NULL; struct virtio_net_config_ll *ll_dev_cur = ll_root; @@ -405,8 +405,8 @@ destroy_device(struct vhost_device_ctx ctx) } } -static void -set_ifname(struct vhost_device_ctx ctx, +void +vhost_set_ifname(struct vhost_device_ctx ctx, const char *if_name, unsigned int if_len) { struct virtio_net *dev; @@ -428,8 +428,8 @@ set_ifname(struct vhost_device_ctx ctx, * This function just returns success at the moment unless * the device hasn't been initialised. */ -static int -set_owner(struct vhost_device_ctx ctx) +int +vhost_set_owner(struct vhost_device_ctx ctx) { struct virtio_net *dev; @@ -443,8 +443,8 @@ set_owner(struct vhost_device_ctx ctx) /* * Called from CUSE IOCTL: VHOST_RESET_OWNER */ -static int -reset_owner(struct vhost_device_ctx ctx) +int +vhost_reset_owner(struct vhost_device_ctx ctx) { struct virtio_net *dev; @@ -464,8 +464,8 @@ reset_owner(struct vhost_device_ctx ctx) * Called from CUSE IOCTL: VHOST_GET_FEATURES * The features that we support are requested. */ -static int -get_features(struct vhost_device_ctx ctx, uint64_t *pu) +int +vhost_get_features(struct vhost_device_ctx ctx, uint64_t *pu) { struct virtio_net *dev; @@ -482,8 +482,8 @@ get_features(struct vhost_device_ctx ctx, uint64_t *pu) * Called from CUSE IOCTL: VHOST_SET_FEATURES * We receive the negotiated features supported by us and the virtio device. */ -static int -set_features(struct vhost_device_ctx ctx, uint64_t *pu) +int +vhost_set_features(struct vhost_device_ctx ctx, uint64_t *pu) { struct virtio_net *dev; uint16_t vhost_hlen; @@ -522,8 +522,9 @@ set_features(struct vhost_device_ctx ctx, uint64_t *pu) * Called from CUSE IOCTL: VHOST_SET_VRING_NUM * The virtio device sends us the size of the descriptor ring. */ -static int -set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state *state) +int +vhost_set_vring_num(struct vhost_device_ctx ctx, + struct vhost_vring_state *state) { struct virtio_net *dev; @@ -624,8 +625,8 @@ numa_realloc(struct virtio_net *dev, int index __rte_unused) * The virtio device sends us the desc, used and avail ring addresses. * This function then converts these to our address space. */ -static int -set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr) +int +vhost_set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr) { struct virtio_net *dev; struct vhost_virtqueue *vq; @@ -686,8 +687,9 @@ set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr) * Called from CUSE IOCTL: VHOST_SET_VRING_BASE * The virtio device sends us the available ring last used index. */ -static int -set_vring_base(struct vhost_device_ctx ctx, struct vhost_vring_state *state) +int +vhost_set_vring_base(struct vhost_device_ctx ctx, + struct vhost_vring_state *state) { struct virtio_net *dev; @@ -706,8 +708,8 @@ set_vring_base(struct vhost_device_ctx ctx, struct vhost_vring_state *state) * Called from CUSE IOCTL: VHOST_GET_VRING_BASE * We send the virtio device our available ring last used index. */ -static int -get_vring_base(struct vhost_device_ctx ctx, uint32_t index, +int +vhost_get_vring_base(struct vhost_device_ctx ctx, uint32_t index, struct vhost_vring_state *state) { struct virtio_net *dev; @@ -729,8 +731,8 @@ get_vring_base(struct vhost_device_ctx ctx, uint32_t index, * The virtio device sends an eventfd to interrupt the guest. This fd gets * copied into our process space. */ -static int -set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file) +int +vhost_set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file) { struct virtio_net *dev; struct vhost_virtqueue *vq; @@ -766,8 +768,8 @@ set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file) * The virtio device sends an eventfd that it can use to notify us. * This fd gets copied into our process space. */ -static int -set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) +int +vhost_set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) { struct virtio_net *dev; struct vhost_virtqueue *vq; @@ -796,8 +798,8 @@ set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file) * At that point we remove the device from the data core. * The device will still exist in the device configuration linked list. */ -static int -set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file) +int +vhost_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file) { struct virtio_net *dev; @@ -824,42 +826,6 @@ set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file) return 0; } -/* - * Function pointers are set for the device operations to allow CUSE to call - * functions when an IOCTL, device_add or device_release is received. - */ -static const struct vhost_net_device_ops vhost_device_ops = { - .new_device = new_device, - .destroy_device = destroy_device, - - .set_ifname = set_ifname, - - .get_features = get_features, - .set_features = set_features, - - .set_vring_num = set_vring_num, - .set_vring_addr = set_vring_addr, - .set_vring_base = set_vring_base, - .get_vring_base = get_vring_base, - - .set_vring_kick = set_vring_kick, - .set_vring_call = set_vring_call, - - .set_backend = set_backend, - - .set_owner = set_owner, - .reset_owner = reset_owner, -}; - -/* - * Called by main to setup callbacks when registering CUSE device. - */ -struct vhost_net_device_ops const * -get_virtio_net_callbacks(void) -{ - return &vhost_device_ops; -} - int rte_vhost_enable_guest_notification(struct virtio_net *dev, uint16_t queue_id, int enable) {