MCPcopy Create free account
hub / github.com/F-Stack/f-stack / vhost_user_setup

Function vhost_user_setup

dpdk/drivers/net/virtio/virtio_user/vhost_user.c:815–875  ·  view source on GitHub ↗

* Set up environment to talk with a vhost user backend. * * @return * - (-1) if fail; * - (0) if succeed. */

Source from the content-addressed store, hash-verified

813 * - (0) if succeed.
814 */
815static int
816vhost_user_setup(struct virtio_user_dev *dev)
817{
818 int fd;
819 int flag;
820 struct sockaddr_un un;
821 struct vhost_user_data *data;
822
823 data = malloc(sizeof(*data));
824 if (!data) {
825 PMD_DRV_LOG(ERR, "(%s) Failed to allocate Vhost-user data", dev->path);
826 return -1;
827 }
828
829 memset(data, 0, sizeof(*data));
830
831 dev->backend_data = data;
832
833 data->vhostfd = -1;
834 data->listenfd = -1;
835
836 fd = socket(AF_UNIX, SOCK_STREAM, 0);
837 if (fd < 0) {
838 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
839 goto err_data;
840 }
841
842 flag = fcntl(fd, F_GETFD);
843 if (flag == -1)
844 PMD_DRV_LOG(WARNING, "fcntl get fd failed, %s", strerror(errno));
845 else if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
846 PMD_DRV_LOG(WARNING, "fcntl set fd failed, %s", strerror(errno));
847
848 memset(&un, 0, sizeof(un));
849 un.sun_family = AF_UNIX;
850 strlcpy(un.sun_path, dev->path, sizeof(un.sun_path));
851
852 if (dev->is_server) {
853 data->listenfd = fd;
854 if (vhost_user_start_server(dev, &un) < 0) {
855 PMD_DRV_LOG(ERR, "virtio-user startup fails in server mode");
856 goto err_socket;
857 }
858 } else {
859 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
860 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
861 goto err_socket;
862 }
863 data->vhostfd = fd;
864 }
865
866 return 0;
867
868err_socket:
869 close(fd);
870err_data:
871 free(data);
872 dev->backend_data = NULL;

Callers

nothing calls this directly

Calls 9

mallocFunction · 0.85
memsetFunction · 0.85
vhost_user_start_serverFunction · 0.70
socketClass · 0.50
fcntlFunction · 0.50
strlcpyFunction · 0.50
connectFunction · 0.50
closeFunction · 0.50
freeFunction · 0.50

Tested by

no test coverage detected